Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycles in chained exceptions

Tags:

java

guava

I'll first quickly motivate the question with my use case. My library needs to expose a Java exception classifier to a framework which it plugs in to. For example:

enum Classification { FATAL, TRANSIENT, UNKNOWN }

Classification classify(Throwable t) {
    if (t instanceof MyTransientException)
        return Classification.TRANSIENT;
    else if (t instanceof MyFatalException)
        return Classification.FATAL;
    else
        return Classification.UNKNOWN;
}

Sometimes, and for reasons out of my control, the passed exception is a wrapper around the one I'm interested in so I want to search the cause chain for it. My initial idea was:

Classification classify(Throwable t) {
    if (t == null)
        return Classification.UNKNOWN;

    if (t instanceof MyTransientException)
        return Classification.TRANSIENT;
    else if (t instanceof MyFatalException)
        return Classification.FATAL;
    else
        return classify(t.getCause());
}

Unfortunately, this could result in infinite recursion if the passed exception has a cycle in its causal chain. It's highly unlikely that such an exception will be passed, and an argument could be made that it's an error elsewhere in the system if such an exception is created, but I'm very uncomfortable with having the possibility of my library being responsible for a production outage if it happens. Throwable's API and javadoc do not explicitly forbid this possibility beyond the idea that cycles are inherently nonsensical in a causal chain.

I noticed that Guava has a @Beta method to extract the causal chain, Throwables.getCausalChain, but its implementation is susceptible to the same issue -- it will wind up throwing an OOME.

I'm planning to use an identity hash set to detect the cycle and mitigate the risk, but I wanted to hear how others view this issue. Do you think I'm being overly defensive? What would you do?

like image 918
Brian Harris Avatar asked Sep 28 '11 15:09

Brian Harris


People also ask

What is chained exception explain with example?

Chained Exception helps to identify a situation in which one exception causes another Exception in an application. For instance, consider a method which throws an ArithmeticException because of an attempt to divide by zero but the actual cause of exception was an I/O error which caused the divisor to be zero.

What are the key methods associated with chained exceptions?

Methods Of Throwable class Which support chained exceptions in java : getCause() method :- This method returns actual cause of an exception. initCause(Throwable cause) method :- This method sets the cause for the calling exception.

What do you mean by chained exception in Java Mcq?

Explanation: In Java, an exception caused by other exceptions is known as a chained exception. Generally, the first exception causes the second exception. It helps in identifying the cause of the exception. In chained exceptions, the debugging information is not discarded.


1 Answers

It's great that you are being so conscientious. But you don't want to get into the business of manufacturing kevlar boots.

If a user does something that would make even Throwable.printStackTrace go into infinite recursion, that user is beyond help. Don't even worry about this.

like image 197
Kevin Bourrillion Avatar answered Oct 05 '22 17:10

Kevin Bourrillion