Google
 
Web unafbapune.blogspot.com

Tuesday, August 05, 2008

 

Root Cause Java Quiz

Can the following code leads to an endless loop ?
    public static Throwable getRootCause(Throwable t) {
if (t == null)
return t;
Throwable cause = t.getCause();

if (cause == null)
return t;
return getRootCause(cause);
}

Answer:

Don't try this at home ;)
        Exception ex1 = new Exception("ex1");
Exception ex2 = new Exception("ex2");
ex1.initCause(ex2);
ex2.initCause(ex1);
getRootCause(ex1);

This page is powered by Blogger. Isn't yours?