Click here to watch in Youtube :
https://www.youtube.com/watch?v=O16v69dCUj8&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial : Java Exception handling (chained Exceptions) |
Java Tutorial : Java Exception handling (chained Exceptions) |
public class MyException extends Exception { public MyException(String message) { super(message); } public MyException(Throwable throwable) { super(throwable); } public MyException(String message, Throwable throwable) { super(message, throwable); } }
public class ChainedExceptionDemo1 { public static void main(String[] args) { try { getValue(); } catch (MyException myException) { System.out.println("Cause = " + myException.getCause()); System.out.println("Message = " + myException.getMessage()); } } public static int getValue() throws MyException { int a = 0; try { a = 10 / 0; } catch (ArithmeticException arithmeticException) { MyException myException = new MyException( arithmeticException.getMessage(), arithmeticException); throw myException; } return a; } }
Cause = java.lang.ArithmeticException: / by zero Message = / by zeroChainedExceptionDemo2.java
public class ChainedExceptionDemo2 { public static void main(String[] args) { try { getValue(); } catch (MyException myException) { System.out.println("Cause = " + myException.getCause()); System.out.println("Message = " + myException.getMessage()); } } public static int getValue() throws MyException { int a = 0; try { a = 10 / 0; } catch (ArithmeticException arithmeticException) { MyException myException = new MyException( arithmeticException.getMessage()); myException.initCause(arithmeticException); throw myException; } return a; } }
Cause = java.lang.ArithmeticException: / by zero Message = / by zeroChainedExceptionDemo3.java
public class ChainedExceptionDemo3 { public static void main(String[] args) { try { getValue(); } catch (MyException myException) { System.out.println("Cause = " + myException.getCause()); System.out.println("Message = " + myException.getMessage()); } } public static int getValue() throws MyException { int a = 0; try { a = 10 / 0; } catch (ArithmeticException arithmeticException) { MyException myException = new MyException( arithmeticException); throw myException; } return a; } }
Cause = java.lang.ArithmeticException: / by zero Message = java.lang.ArithmeticException: / by zero
https://sites.google.com/site/javaee4321/java/ExceptionDemo_chained_exceptions_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_chained_exceptions_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4babce59a384cb4ab41f12b03cd7d11227a4b186/BasicJava/ExceptionDemo_chained_exceptions_App/?at=master
See also:
No comments:
Post a Comment