|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
@@ -88,15 +88,24 @@ public static void main(java.lang.String[] unused) throws Exception {
|
88 | 88 | Object junk = testMethod.invoke(null, new Object [0]);
|
89 | 89 | throw new RuntimeException("InvocationTargetException should be thrown");
|
90 | 90 | } catch (InvocationTargetException ite) {
|
91 |
| - Throwable targetException = ite.getTargetException(); |
92 |
| - if (targetException instanceof OutOfMemoryError) { |
93 |
| - System.out.println("OutOfMemoryError thrown as expected."); |
94 |
| - System.out.println("Test passed."); |
95 |
| - } else { |
96 |
| - throw new RuntimeException("Unexpected InvocationTargetException: " + targetException); |
| 91 | + // We may not directly get OOME but it could have caused |
| 92 | + // secondary exceptions, so walk the chain of exceptions |
| 93 | + // and see if there is an OOME somewhere. |
| 94 | + for (Throwable cause = ite.getTargetException(); |
| 95 | + cause != null; |
| 96 | + cause = cause.getCause()) { |
| 97 | + if (cause instanceof OutOfMemoryError) { |
| 98 | + System.out.println("OutOfMemoryError thrown as expected."); |
| 99 | + ite.printStackTrace(System.out); |
| 100 | + System.out.println("Test passed."); |
| 101 | + return; |
| 102 | + } |
97 | 103 | }
|
| 104 | + |
| 105 | + throw new RuntimeException("Unexpected InvocationTargetException: ", |
| 106 | + ite.getTargetException()); |
98 | 107 | } catch (Exception exception) {
|
99 |
| - throw new RuntimeException("Unexpected exception: " + exception); |
| 108 | + throw new RuntimeException("Unexpected exception: ", exception); |
100 | 109 | }
|
101 | 110 | }
|
102 | 111 | }
|
0 commit comments