Christoffer Quist Adamsen | 28ece0f | 2018-06-07 09:16:04 +0200 | [diff] [blame] | 1 | // Copyright (c) 2018, the R8 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 | package classmerging; |
| 6 | |
| 7 | public class ExceptionTest { |
| 8 | public static void main(String[] args) { |
| 9 | // The following will lead to a catch handler for ExceptionA, which is merged into ExceptionB. |
| 10 | try { |
Christoffer Quist Adamsen | 95dd66b | 2018-06-11 09:12:45 +0200 | [diff] [blame] | 11 | doSomethingThatMightThrowExceptionB(); |
| 12 | doSomethingThatMightThrowException2(); |
| 13 | } catch (ExceptionB exception) { |
| 14 | System.out.println("Caught exception: " + exception.getMessage()); |
Christoffer Quist Adamsen | 28ece0f | 2018-06-07 09:16:04 +0200 | [diff] [blame] | 15 | } catch (ExceptionA exception) { |
| 16 | System.out.println("Caught exception: " + exception.getMessage()); |
Christoffer Quist Adamsen | 95dd66b | 2018-06-11 09:12:45 +0200 | [diff] [blame] | 17 | } catch (Exception2 exception) { |
| 18 | System.out.println("Caught exception: " + exception.getMessage()); |
| 19 | } catch (Exception1 exception) { |
| 20 | System.out.println("Caught exception: " + exception.getMessage()); |
Christoffer Quist Adamsen | 28ece0f | 2018-06-07 09:16:04 +0200 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | |
Christoffer Quist Adamsen | 95dd66b | 2018-06-11 09:12:45 +0200 | [diff] [blame] | 24 | private static void doSomethingThatMightThrowExceptionB() throws ExceptionB { |
| 25 | throw new ExceptionB("Ouch!"); |
| 26 | } |
| 27 | |
| 28 | private static void doSomethingThatMightThrowException2() throws Exception2 { |
| 29 | throw new Exception2("Ouch!"); |
| 30 | } |
| 31 | |
Christoffer Quist Adamsen | 28ece0f | 2018-06-07 09:16:04 +0200 | [diff] [blame] | 32 | // Will be merged into ExceptionB when class merging is enabled. |
| 33 | public static class ExceptionA extends Exception { |
| 34 | public ExceptionA(String message) { |
| 35 | super(message); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public static class ExceptionB extends ExceptionA { |
| 40 | public ExceptionB(String message) { |
| 41 | super(message); |
| 42 | } |
| 43 | } |
Christoffer Quist Adamsen | 95dd66b | 2018-06-11 09:12:45 +0200 | [diff] [blame] | 44 | |
| 45 | public static class Exception1 extends Exception { |
| 46 | public Exception1(String message) { |
| 47 | super(message); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public static class Exception2 extends Exception1 { |
| 52 | public Exception2(String message) { |
| 53 | super(message); |
| 54 | } |
| 55 | } |
Christoffer Quist Adamsen | 28ece0f | 2018-06-07 09:16:04 +0200 | [diff] [blame] | 56 | } |