Thursday 30 June 2016

Java Tutorial : Java Exception handling (The try block)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=JvJvqOdL9Is&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Exception handling (The try block) 
Java Tutorial : Java Exception handling (The try block) 
Java Tutorial : Java Exception handling (The try block) 
TryBlockDemo1.java
public class TryBlockDemo1
{

    public static void main(String[] args)
    {
        
        try
        {
            String str = null;
            
            // This line will throw NullPointerException
            System.out.println(str.length());
        }
        
        catch (NullPointerException nullPointerException)
        {
            nullPointerException.printStackTrace();
        }
        
        try
        {
        
            // This line will throw ArithmeticException
            int a = 5/0;
            
        }
        
        catch (ArithmeticException arithmeticException)
        {
            arithmeticException.printStackTrace();
        }
        
        System.out.println("Normal flow---");       

    }
}
Output
java.lang.NullPointerException
    at TryBlockDemo1.main(TryBlockDemo1.java:13)
java.lang.ArithmeticException: / by zero
    at TryBlockDemo1.main(TryBlockDemo1.java:25)
Normal flow---
TryBlockDemo2.java
public class TryBlockDemo2
{

    public static void main(String[] args)
    {

        try
        {
            String str = null;

            // This line will throw NullPointerException
            System.out.println(str.length());

            // This line will throw ArithmeticException
            int a = 5 / 0;

        }

        catch (ArithmeticException arithmeticException)
        {
            arithmeticException.printStackTrace();
        }

        catch (NullPointerException nullPointerException)
        {
            nullPointerException.printStackTrace();
            
        }

        System.out.println("Normal flow---");

    }
}
Output
java.lang.NullPointerException
    at TryBlockDemo2.main(TryBlockDemo2.java:12)
Normal flow---
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ExceptionDemo_The_try_block_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_The_try_block_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4babce59a384cb4ab41f12b03cd7d11227a4b186/BasicJava/ExceptionDemo_The_try_block_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