| 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 MyException1 { | 6 class MyException1 { |
| 7 const MyException1([String message = "1"]) : message_ = message; | 7 const MyException1([String message = "1"]) : message_ = message; |
| 8 final String message_; | 8 final String message_; |
| 9 } | 9 } |
| 10 | 10 |
| 11 class MyException2 { | 11 class MyException2 { |
| 12 const MyException2([String message = "2"]) : message_ = message; | 12 const MyException2([String message = "2"]) : message_ = message; |
| 13 final String message_; | 13 final String message_; |
| 14 } | 14 } |
| 15 | 15 |
| 16 class MyException3 { | 16 class MyException3 { |
| 17 const MyException3([String message = "3"]) : message_ = message; | 17 const MyException3([String message = "3"]) : message_ = message; |
| 18 final String message_; | 18 final String message_; |
| 19 } | 19 } |
| 20 | 20 |
| 21 class Helper { | 21 class Helper { |
| 22 static int f1(int i) { | 22 static int f1(int i) { |
| 23 try { | 23 try { |
| 24 int j; | 24 int j; |
| 25 j = func(); | 25 j = func(); |
| 26 } catch (MyException3 exception) { | 26 } on MyException3 catch (exception) { |
| 27 i = 300; | 27 i = 300; |
| 28 print(exception.message_); | 28 print(exception.message_); |
| 29 } catch (MyException2 exception) { | 29 } on MyException2 catch (exception) { |
| 30 i = 200; | 30 i = 200; |
| 31 print(exception.message_); | 31 print(exception.message_); |
| 32 } catch (MyException1 exception) { | 32 } on MyException1 catch (exception) { |
| 33 i = 100; | 33 i = 100; |
| 34 print(exception.message_); | 34 print(exception.message_); |
| 35 } finally { | 35 } finally { |
| 36 i = i + 800; | 36 i = i + 800; |
| 37 } | 37 } |
| 38 return i; | 38 return i; |
| 39 } | 39 } |
| 40 | 40 |
| 41 // No catch in the same function for the type of exception being thrown | 41 // No catch in the same function for the type of exception being thrown |
| 42 // in the try block here. We expect the handler if checks to fall thru and | 42 // in the try block here. We expect the handler if checks to fall thru and |
| 43 // implicit rethrow to happen. | 43 // implicit rethrow to happen. |
| 44 static int func() { | 44 static int func() { |
| 45 int i = 0; | 45 int i = 0; |
| 46 try { | 46 try { |
| 47 while (i < 10) { | 47 while (i < 10) { |
| 48 i++; | 48 i++; |
| 49 } | 49 } |
| 50 if (i > 0) { | 50 if (i > 0) { |
| 51 throw new MyException1("Test for MyException1 being thrown"); | 51 throw new MyException1("Test for MyException1 being thrown"); |
| 52 } | 52 } |
| 53 } catch (MyException3 exception) { | 53 } on MyException3 catch (exception) { |
| 54 i = 300; | 54 i = 300; |
| 55 print(exception.message_); | 55 print(exception.message_); |
| 56 } catch (MyException2 exception) { | 56 } on MyException2 catch (exception) { |
| 57 i = 200; | 57 i = 200; |
| 58 print(exception.message_); | 58 print(exception.message_); |
| 59 } | 59 } |
| 60 return i; | 60 return i; |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 class Throw5Test { | 64 class Throw5Test { |
| 65 static testMain() { | 65 static testMain() { |
| 66 Expect.equals(900, Helper.f1(1)); | 66 Expect.equals(900, Helper.f1(1)); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 main() { | 70 main() { |
| 71 Throw5Test.testMain(); | 71 Throw5Test.testMain(); |
| 72 } | 72 } |
| OLD | NEW |