Thursday, 30 June 2016

Java Tutorial : Java Exception handling (chained Exceptions)


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) 
MyException.java
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);
    }

}
ChainedExceptionDemo1.java
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;
    }

}
Output
Cause = java.lang.ArithmeticException: / by zero
Message = / by zero
ChainedExceptionDemo2.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;
    }

}
Output
Cause = java.lang.ArithmeticException: / by zero
Message = / by zero
ChainedExceptionDemo3.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;
    }

}
Output
Cause = java.lang.ArithmeticException: / by zero
Message = java.lang.ArithmeticException: / by zero
Click the below link to download the code:
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:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment