OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class MyException { | 6 class MyException { |
7 const MyException(String message) : message_ = message; | 7 const MyException(String message) : message_ = message; |
8 final String message_; | 8 final String message_; |
9 } | 9 } |
10 | 10 |
11 class Helper { | 11 class Helper { |
12 static int f1(int i) { | 12 static int f1(int i) { |
13 try { | 13 try { |
14 i = func(); | 14 i = func(); |
15 i = 10; | 15 i = 10; |
16 } catch (MyException exception, var stacktrace) { | 16 } on MyException catch (exception, stacktrace) { |
17 i = 50; | 17 i = 50; |
18 print(exception.message_); | 18 print(exception.message_); |
19 Expect.equals((stacktrace != null), true); | 19 Expect.equals((stacktrace != null), true); |
20 print(stacktrace); | 20 print(stacktrace); |
21 } | 21 } |
22 try { | 22 try { |
23 int j; | 23 int j; |
24 i = func1(); | 24 i = func1(); |
25 i = 200; | 25 i = 200; |
26 } catch (MyException exception, var stacktrace) { | 26 } on MyException catch (exception, stacktrace) { |
27 i = 50; | 27 i = 50; |
28 print(exception.message_); | 28 print(exception.message_); |
29 Expect.equals((stacktrace != null), true); | 29 Expect.equals((stacktrace != null), true); |
30 print(stacktrace); | 30 print(stacktrace); |
31 } | 31 } |
32 try { | 32 try { |
33 int j; | 33 int j; |
34 i = func2(); | 34 i = func2(); |
35 i = 200; | 35 i = 200; |
36 } catch (MyException exception, var stacktrace) { | 36 } on MyException catch (exception, stacktrace) { |
37 i = 50; | 37 i = 50; |
38 print(exception.message_); | 38 print(exception.message_); |
39 Expect.equals((stacktrace != null), true); | 39 Expect.equals((stacktrace != null), true); |
40 print(stacktrace); | 40 print(stacktrace); |
41 } finally { | 41 } finally { |
42 i = i + 800; | 42 i = i + 800; |
43 } | 43 } |
44 return i; | 44 return i; |
45 } | 45 } |
46 | 46 |
47 static int func() { | 47 static int func() { |
48 int i = 0; | 48 int i = 0; |
49 while (i < 10) { | 49 while (i < 10) { |
50 i++; | 50 i++; |
51 } | 51 } |
52 if (i > 0) { | 52 if (i > 0) { |
53 throw new MyException("Exception Test for stack trace being printed"); | 53 throw new MyException("Exception Test for stack trace being printed"); |
54 } | 54 } |
55 return 10; | 55 return 10; |
56 } | 56 } |
57 | 57 |
58 static int func1() { | 58 static int func1() { |
59 try { | 59 try { |
60 func(); | 60 func(); |
61 } catch (MyException exception) { | 61 } on MyException catch (exception) { |
62 throw new MyException("Exception Test for stack trace being printed");; | 62 throw new MyException("Exception Test for stack trace being printed");; |
63 } | 63 } |
64 return 10; | 64 return 10; |
65 } | 65 } |
66 | 66 |
67 static int func2() { | 67 static int func2() { |
68 try { | 68 try { |
69 func(); | 69 func(); |
70 } catch (MyException exception) { | 70 } on MyException catch (exception) { |
71 throw; | 71 throw; |
72 } | 72 } |
73 return 10; | 73 return 10; |
74 } | 74 } |
75 | 75 |
76 } | 76 } |
77 | 77 |
78 class StackTraceTest { | 78 class StackTraceTest { |
79 static testMain() { | 79 static testMain() { |
80 Expect.equals(850, Helper.f1(1)); | 80 Expect.equals(850, Helper.f1(1)); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 main() { | 84 main() { |
85 StackTraceTest.testMain(); | 85 StackTraceTest.testMain(); |
86 } | 86 } |
OLD | NEW |