OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.core; | |
6 | |
7 // Exceptions are thrown either by the VM or from Dart code. | |
8 | |
9 /** | |
10 * A marker interface implemented by all core library exceptions. | |
11 * | |
12 * An [Exception] is intended to convey information to the user about a failure, | |
13 * so that the error can be addressed programmatically. It is intended to be | |
14 * caught, and it should contain useful data fields. | |
15 * | |
16 * Creating instances of [Exception] directly with [:new Exception("message"):] | |
17 * is discouraged, and only included as a temporary measure during development, | |
18 * until the actual exceptions used by a library are done. | |
19 */ | |
20 abstract class Exception { | |
21 factory Exception([var message]) => new _Exception(message); | |
22 } | |
23 | |
24 | |
25 /** Default implementation of [Exception] which carries a message. */ | |
26 class _Exception implements Exception { | |
27 final message; | |
28 | |
29 _Exception([this.message]); | |
30 | |
31 String toString() { | |
32 if (message == null) return "Exception"; | |
33 return "Exception: $message"; | |
34 } | |
35 } | |
36 | |
37 | |
38 /** | |
39 * Exception thrown when a string or some other data does not have an expected | |
40 * format and cannot be parsed or processed. | |
41 */ | |
42 class FormatException implements Exception { | |
43 /** | |
44 * A message describing the format error. | |
45 */ | |
46 final String message; | |
47 | |
48 /** | |
49 * The actual source input which caused the error. | |
50 * | |
51 * This is usually a [String], but can be other types too. | |
52 * If it is a string, parts of it may be included in the [toString] message. | |
53 * | |
54 * The source is `null` if omitted or unknown. | |
55 */ | |
56 final source; | |
57 | |
58 /** | |
59 * The offset in [source] where the error was detected. | |
60 * | |
61 * A zero-based offset into the source that marks the format error causing | |
62 * this exception to be created. If `source` is a string, this should be a | |
63 * string index in the range `0 <= offset <= source.length`. | |
64 * | |
65 * If input is a string, the [toString] method may represent this offset as | |
66 * a line and character position. The offset should be inside the string, | |
67 * or at the end of the string. | |
68 * | |
69 * May be omitted. If present, [source] should also be present if possible. | |
70 */ | |
71 final int offset; | |
72 | |
73 /** | |
74 * Creates a new FormatException with an optional error [message]. | |
75 * | |
76 * Optionally also supply the actual [source] with the incorrect format, | |
77 * and the [offset] in the format where a problem was detected. | |
78 */ | |
79 const FormatException([this.message = "", this.source, this.offset]); | |
80 | |
81 /** | |
82 * Returns a description of the format exception. | |
83 * | |
84 * The description always contains the [message]. | |
85 * | |
86 * If [source] is present and is a string, the description will contain | |
87 * (at least a part of) the source. | |
88 * If [offset] is also provided, the part of the source included will | |
89 * contain that offset, and the offset will be marked. | |
90 * | |
91 * If the source is a string and it contains a line break before offset, | |
92 * only the line containing offset will be included, and its line number | |
93 * will also be part of the description. Line and character offsets are | |
94 * 1-based. | |
95 */ | |
96 String toString() { | |
97 String report = "FormatException"; | |
98 if (message != null && "" != message) { | |
99 report = "$report: $message"; | |
100 } | |
101 int offset = this.offset; | |
102 if (source is! String) { | |
103 if (offset != null) { | |
104 report += " (at offset $offset)"; | |
105 } | |
106 return report; | |
107 } | |
108 if (offset != null && (offset < 0 || offset > source.length)) { | |
109 offset = null; | |
110 } | |
111 // Source is string and offset is null or valid. | |
112 if (offset == null) { | |
113 String source = this.source; | |
114 if (source.length > 78) { | |
115 source = source.substring(0, 75) + "..."; | |
116 } | |
117 return "$report\n$source"; | |
118 } | |
119 int lineNum = 1; | |
120 int lineStart = 0; | |
121 bool lastWasCR; | |
122 for (int i = 0; i < offset; i++) { | |
123 int char = source.codeUnitAt(i); | |
124 if (char == 0x0a) { | |
125 if (lineStart != i || !lastWasCR) { | |
126 lineNum++; | |
127 } | |
128 lineStart = i + 1; | |
129 lastWasCR = false; | |
130 } else if (char == 0x0d) { | |
131 lineNum++; | |
132 lineStart = i + 1; | |
133 lastWasCR = true; | |
134 } | |
135 } | |
136 if (lineNum > 1) { | |
137 report += " (at line $lineNum, character ${offset - lineStart + 1})\n"; | |
138 } else { | |
139 report += " (at character ${offset + 1})\n"; | |
140 } | |
141 int lineEnd = source.length; | |
142 for (int i = offset; i < source.length; i++) { | |
143 int char = source.codeUnitAt(i); | |
144 if (char == 0x0a || char == 0x0d) { | |
145 lineEnd = i; | |
146 break; | |
147 } | |
148 } | |
149 int length = lineEnd - lineStart; | |
150 int start = lineStart; | |
151 int end = lineEnd; | |
152 String prefix = ""; | |
153 String postfix = ""; | |
154 if (length > 78) { | |
155 // Can't show entire line. Try to anchor at the nearest end, if | |
156 // one is within reach. | |
157 int index = offset - lineStart; | |
158 if (index < 75) { | |
159 end = start + 75; | |
160 postfix = "..."; | |
161 } else if (end - offset < 75) { | |
162 start = end - 75; | |
163 prefix = "..."; | |
164 } else { | |
165 // Neither end is near, just pick an area around the offset. | |
166 start = offset - 36; | |
167 end = offset + 36; | |
168 prefix = postfix = "..."; | |
169 } | |
170 } | |
171 String slice = source.substring(start, end); | |
172 int markOffset = offset - start + prefix.length; | |
173 return "$report$prefix$slice$postfix\n${" " * markOffset}^\n"; | |
174 } | |
175 } | |
176 | |
177 // Exception thrown when doing integer division with a zero divisor. | |
178 class IntegerDivisionByZeroException implements Exception { | |
179 const IntegerDivisionByZeroException(); | |
180 String toString() => "IntegerDivisionByZeroException"; | |
181 } | |
OLD | NEW |