提交 51593e7d 编写于 作者: D darcy

8012044: Give more information about self-suppression from Throwable.addSuppressed

Reviewed-by: alanb, dholmes
上级 ff97235b
/*
* Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -453,9 +453,10 @@ public class Throwable implements Serializable {
*/
public synchronized Throwable initCause(Throwable cause) {
if (this.cause != this)
throw new IllegalStateException("Can't overwrite cause");
throw new IllegalStateException("Can't overwrite cause with " +
Objects.toString(cause, "a null"), this);
if (cause == this)
throw new IllegalArgumentException("Self-causation not permitted");
throw new IllegalArgumentException("Self-causation not permitted", this);
this.cause = cause;
return this;
}
......@@ -1039,7 +1040,7 @@ public class Throwable implements Serializable {
*/
public final synchronized void addSuppressed(Throwable exception) {
if (exception == this)
throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);
if (exception == null)
throw new NullPointerException(NULL_CAUSE_MESSAGE);
......
/*
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -26,7 +26,7 @@ import java.util.*;
/*
* @test
* @bug 6911258 6962571 6963622 6991528 7005628
* @bug 6911258 6962571 6963622 6991528 7005628 8012044
* @summary Basic tests of suppressed exceptions
* @author Joseph D. Darcy
*/
......@@ -40,6 +40,7 @@ public class SuppressedExceptions {
serializationTest();
selfReference();
noModification();
initCausePlumbing();
}
private static void noSelfSuppression() {
......@@ -48,7 +49,9 @@ public class SuppressedExceptions {
throwable.addSuppressed(throwable);
throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown.");
} catch (IllegalArgumentException iae) {
; // Expected
// Expected to be here
if (iae.getCause() != throwable)
throw new RuntimeException("Bad cause after self-suppresion.");
}
}
......@@ -208,4 +211,36 @@ public class SuppressedExceptions {
super("The medium.", null, enableSuppression, true);
}
}
private static void initCausePlumbing() {
Throwable t1 = new Throwable();
Throwable t2 = new Throwable("message", t1);
Throwable t3 = new Throwable();
try {
t2.initCause(t3);
throw new RuntimeException("Shouldn't reach.");
} catch (IllegalStateException ise) {
if (ise.getCause() != t2)
throw new RuntimeException("Unexpected cause in ISE", ise);
Throwable[] suppressed = ise.getSuppressed();
if (suppressed.length != 0)
throw new RuntimeException("Bad suppression in ISE", ise);
}
try {
t2.initCause(null);
throw new RuntimeException("Shouldn't reach.");
} catch (IllegalStateException ise) {
; // Expected; don't want an NPE.
}
try {
t3.initCause(t3);
throw new RuntimeException("Shouldn't reach.");
} catch (IllegalArgumentException iae) {
if (iae.getCause() != t3)
throw new RuntimeException("Unexpected cause in ISE", iae);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册