OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Dart test program for testing throw statement | 4 // Dart test program for testing throw statement |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 class MyException { | 8 class MyException { |
9 const MyException(String message) : message_ = message; | 9 const MyException(String message) : message_ = message; |
10 final String message_; | 10 final String message_; |
11 } | 11 } |
12 | 12 |
13 class Helper1 { | 13 class Helper1 { |
14 static int func1() { | 14 static int func1() { |
15 return func2(); | 15 return func2(); |
16 } | 16 } |
17 | |
18 static int func2() { | 17 static int func2() { |
19 return func3(); | 18 return func3(); |
20 } | 19 } |
21 | |
22 static int func3() { | 20 static int func3() { |
23 return func4(); | 21 return func4(); |
24 } | 22 } |
25 | |
26 static int func4() { | 23 static int func4() { |
27 var i = 0; | 24 var i = 0; |
28 try { | 25 try { |
29 i = 10; | 26 i = 10; |
30 func5(); | 27 func5(); |
31 } on ArgumentError catch (e) { | 28 } on ArgumentError catch (e) { |
32 i = 100; | 29 i = 100; |
33 Expect.isNotNull(e.stackTrace, "Errors need a stackTrace on throw"); | 30 Expect.isNotNull(e.stackTrace, "Errors need a stackTrace on throw"); |
34 } | 31 } |
35 return i; | 32 return i; |
36 } | 33 } |
37 | |
38 static void func5() { | 34 static void func5() { |
39 // Throw an Error. | 35 // Throw an Error. |
40 throw new ArgumentError("ArgumentError in func5"); | 36 throw new ArgumentError("ArgumentError in func5"); |
41 } | 37 } |
42 } | 38 } |
43 | 39 |
44 class Helper2 { | 40 class Helper2 { |
45 static int func1() { | 41 static int func1() { |
46 return func2(); | 42 return func2(); |
47 } | 43 } |
48 | |
49 static int func2() { | 44 static int func2() { |
50 return func3(); | 45 return func3(); |
51 } | 46 } |
52 | |
53 static int func3() { | 47 static int func3() { |
54 return func4(); | 48 return func4(); |
55 } | 49 } |
56 | |
57 static int func4() { | 50 static int func4() { |
58 var i = 0; | 51 var i = 0; |
59 try { | 52 try { |
60 i = 10; | 53 i = 10; |
61 func5(); | 54 func5(); |
62 } on ArgumentError catch (e, s) { | 55 } on ArgumentError catch (e, s) { |
63 i = 200; | 56 i = 200; |
64 Expect.isNotNull(e.stackTrace, "Errors need a stackTrace on throw"); | 57 Expect.isNotNull(e.stackTrace, "Errors need a stackTrace on throw"); |
65 Expect.equals(e.stackTrace.toString(), s.toString()); | 58 Expect.equals(e.stackTrace.toString(), s.toString()); |
66 } | 59 } |
67 return i; | 60 return i; |
68 } | 61 } |
69 | |
70 static List func5() { | 62 static List func5() { |
71 // Throw an Error. | 63 // Throw an Error. |
72 throw new ArgumentError("ArgumentError in func5"); | 64 throw new ArgumentError("ArgumentError in func5"); |
73 } | 65 } |
74 } | 66 } |
75 | 67 |
76 class Helper3 { | 68 class Helper3 { |
77 static int func1() { | 69 static int func1() { |
78 return func2(); | 70 return func2(); |
79 } | 71 } |
80 | |
81 static int func2() { | 72 static int func2() { |
82 return func3(); | 73 return func3(); |
83 } | 74 } |
84 | |
85 static int func3() { | 75 static int func3() { |
86 return func4(); | 76 return func4(); |
87 } | 77 } |
88 | |
89 static int func4() { | 78 static int func4() { |
90 var i = 0; | 79 var i = 0; |
91 try { | 80 try { |
92 i = 10; | 81 i = 10; |
93 func5(); | 82 func5(); |
94 } on MyException catch (e) { | 83 } on MyException catch (e) { |
95 i = 300; | 84 i = 300; |
96 try { | 85 try { |
97 // There should be no stackTrace in this normal exception object. | 86 // There should be no stackTrace in this normal exception object. |
98 // We should get a NoSuchMethodError. | 87 // We should get a NoSuchMethodError. |
99 var trace = e.stackTrace; //# static type warning | 88 var trace = e.stackTrace; //# static type warning |
100 } on NoSuchMethodError catch (e) { | 89 } on NoSuchMethodError catch (e) { |
101 Expect.isNotNull(e.stackTrace, "Error needs a stackTrace on throw"); | 90 Expect.isNotNull(e.stackTrace, "Error needs a stackTrace on throw"); |
102 } | 91 } |
103 } | 92 } |
104 return i; | 93 return i; |
105 } | 94 } |
106 | |
107 static List func5() { | 95 static List func5() { |
108 // Throw an Exception (any random object). | 96 // Throw an Exception (any random object). |
109 throw new MyException("MyException in func5"); | 97 throw new MyException("MyException in func5"); |
110 } | 98 } |
111 } | 99 } |
112 | 100 |
113 class ErrorStackTraceTest { | 101 class ErrorStackTraceTest { |
114 static testMain() { | 102 static testMain() { |
115 Expect.equals(100, Helper1.func1()); | 103 Expect.equals(100, Helper1.func1()); |
116 Expect.equals(200, Helper2.func1()); | 104 Expect.equals(200, Helper2.func1()); |
117 Expect.equals(300, Helper3.func1()); | 105 Expect.equals(300, Helper3.func1()); |
118 } | 106 } |
119 } | 107 } |
120 | 108 |
121 main() { | 109 main() { |
122 ErrorStackTraceTest.testMain(); | 110 ErrorStackTraceTest.testMain(); |
123 } | 111 } |
OLD | NEW |