Java Throwable initCause() methodThe initCause() method of Java Throwable class is used to initialize the cause of the calling Throwable with the specified cause. This method can be called only once. If the calling Throwable is created by using Throwable(Throwable) or Throwable(String, Throwable), then this method cannot be called even once. SyntaxParameterA Throwable instance returned by getCause() method, which is to be initialized to the calling Throwable as a cause. ReturnA reference to the calling Throwable's instance. Example 1Test it NowOutput: Cause : java.lang.ArithmeticException: / by zero Example 2Test it NowOutput: Exception in thread "main" javaException: This is new java Exception!! at ThrowableInitCauseExample2.func1(ThrowableInitCauseExample2.java:14) at ThrowableInitCauseExample2.main(ThrowableInitCauseExample2.java:4) Caused by: newException: This is another new exception!! at ThrowableInitCauseExample2.func2(ThrowableInitCauseExample2.java:20) at ThrowableInitCauseExample2.func1(ThrowableInitCauseExample2.java:12) ... 1 more
Next TopicJava Throwable
|