Online Java Compiler By
JavaTpoint.com
public class ThrowableInitCauseExample2 { public static void main(String[] args)throws Throwable { try{ func1(); }catch(Exception e){ System.out.println("Cause : "+e.getCause()); } } public static void func1() throws javaException{ try{ func2(); }catch(newException n){ javaException j1 = new javaException(); j1.initCause(n); throw j1; } } public static void func2() throws newException{ throw new newException(); } } class javaException extends Throwable{ javaException(){ super("This is new java Exception!!"); } } class newException extends Throwable{ newException(){ super("This is another new exception!!"); } }
Output