> I still find the use of addSuppressed in initCause to be questionable. 
> Given:
> 
> catch(SomeException s) {
>        sharedException.initCause(s); // oops already has a cause
> throw sharedException;
> }
> 
> then the ISE isn't suppressing 's', but replacing/suppressing 
> sharedException in my view, as it is what would have been thrown had the 
> ISE not occurred.
I agree. I think makes sense to swap the ISE cause and suppressed arguments 
because the root cause should be the most important throwable to see in a log 
file.
In David's example, that would be 's' or the root cause of 's'. Also when the 
two arguments are swapped the line numbers are in descending order.
 
===========JDK 7 testcase===========================
public static void main(String[] args) throws Exception {
 final Throwable cause = new NoClassDefFoundError();
 final Throwable THIS = new ClassNotFoundException();
 try {
            THIS.initCause(cause); //It's a trap!!
 } catch (IllegalStateException ISE) {
            ISE.initCause(cause); //swapped
            ISE.addSuppressed(THIS); //swapped
            ISE.printStackTrace();
 }
 }
 
java.lang.IllegalStateException: Can't overwrite cause
 at java.lang.Throwable.initCause(Throwable.java:456)
 at Main.main(Main.java:33)
 Suppressed: java.lang.ClassNotFoundException
  at Main.main(Main.java:31)
Caused by: java.lang.NoClassDefFoundError
 at Main.main(Main.java:30)
===============================================
 
Jason                                     

Reply via email to