Throwable.addSuppressed error conditions -- use the exception as the cause?

Joe Darcy joe.darcy at oracle.com
Thu Apr 11 20:48:21 UTC 2013


Hello,

I've filed

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

for this issue; it should be viewable on bugs.sun.com within a day or so.

Cheers,

-Joe

On 04/08/2013 04:54 PM, Steven Schlansker wrote:
> Today I encountered "java.lang.IllegalArgumentException: Self-suppression not permitted" from Throwable.addSuppressed.
>
> My first surprise is that the try-with-resources block can throw this exception; it is very confusing to have auto-generated code throw exceptions.  But beyond that, it is impossible to figure out *which* exception caused the problem.  If you have multiple resources acquired in the try-with-resources, it could be any of them.
>
> Would it be reasonable to change
>
>      public final synchronized void addSuppressed(Throwable exception) {
>          if (exception == this)
>              throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
>
> to
>
>      public final synchronized void addSuppressed(Throwable exception) {
>          if (exception == this)
>              throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, this);
>
> so that when you get this exception it at least points to one of the throw sites that actually caused the problem?  Otherwise the only stack trace you have is in auto-generated code and you are left scratching your head, wondering where it came from.  I believe this would increase the debuggability of the try-with-resources construct and there's no immediately obvious downside.
>
> I'm not the only one who was confused by this behavior:
> http://stackoverflow.com/questions/12103126/what-on-earth-is-self-suppression-not-permitted-and-why-is-javac-generating-co
>
>




More information about the core-libs-dev mailing list