test/java/lang/Throwable/SuppressedExceptions.java

Print this page




 176 
 177     private static void noModification() {
 178         Throwable t = new NoSuppression(false);
 179 
 180         Throwable[] t0 = t.getSuppressed();
 181         if (t0.length != 0)
 182             throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
 183 
 184         t.addSuppressed(new ArithmeticException());
 185 
 186         // Make sure a suppressed exception did *not* get added.
 187         t0 = t.getSuppressed();
 188         if (t0.length != 0)
 189             throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
 190 
 191         Throwable suppressed = new ArithmeticException();
 192         t = new NoSuppression(true); // Suppression enabled
 193         // Make sure addSuppressed(null) throws an NPE
 194         try {
 195             t.addSuppressed(null);

 196         } catch(NullPointerException e) {
 197             ; // Expected
 198         }
 199         t.addSuppressed(suppressed);
 200         t0 = t.getSuppressed();
 201         if (t0.length != 1 || t0[0] != suppressed)
 202             throw new RuntimeException("Expected suppression did not occur.");
 203     }
 204 
 205     private static class NoSuppression extends Throwable {
 206         public NoSuppression(boolean enableSuppression) {
 207             super("The medium.", null, enableSuppression);
 208         }
 209     }
 210 }


 176 
 177     private static void noModification() {
 178         Throwable t = new NoSuppression(false);
 179 
 180         Throwable[] t0 = t.getSuppressed();
 181         if (t0.length != 0)
 182             throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
 183 
 184         t.addSuppressed(new ArithmeticException());
 185 
 186         // Make sure a suppressed exception did *not* get added.
 187         t0 = t.getSuppressed();
 188         if (t0.length != 0)
 189             throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
 190 
 191         Throwable suppressed = new ArithmeticException();
 192         t = new NoSuppression(true); // Suppression enabled
 193         // Make sure addSuppressed(null) throws an NPE
 194         try {
 195             t.addSuppressed(null);
 196             throw new RuntimeException("NPE not thrown!");
 197         } catch(NullPointerException e) {
 198             ; // Expected
 199         }
 200         t.addSuppressed(suppressed);
 201         t0 = t.getSuppressed();
 202         if (t0.length != 1 || t0[0] != suppressed)
 203             throw new RuntimeException("Expected suppression did not occur.");
 204     }
 205 
 206     private static class NoSuppression extends Throwable {
 207         public NoSuppression(boolean enableSuppression) {
 208             super("The medium.", null, enableSuppression, true);
 209         }
 210     }
 211 }