Tuesday 21 June 2016

Java Tutorial : Java Exception handling (multiple catch blocks)


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

Click the below Image to Enlarge
Java Tutorial : Java Exception handling (multiple catch blocks) 
ExceptionDemo1.java
import java.util.ArrayList;
import java.util.Scanner;

public class ExceptionDemo1
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the index : ");
        int index = scanner.nextInt();
        scanner.close();

        ArrayList<String> list = new ArrayList<String>();
        list.add("Dave");
        list.add(null);
        System.out.println("list = " + list);

        String str = list.get(index);
        System.out.println("str = " + str);
        System.out.println("length = " + str.length());
        System.out.println("Normal flow..");
    }

}
Output
Enter the index : 8
list = [Dave, null]Exception in thread "main" 
        java.lang.IndexOutOfBoundsException: Index: 8, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at ExceptionDemo1.main(ExceptionDemo1.java:18)

------------------------------------------------------------

Enter the index : 1
list = [Dave, null]
str = null
Exception in thread "main" java.lang.NullPointerException
    at ExceptionDemo1.main(ExceptionDemo1.java:20)
ExceptionDemo2.java
import java.util.ArrayList;
import java.util.Scanner;

public class ExceptionDemo2
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the index : ");
        int index = scanner.nextInt();
        scanner.close();

        ArrayList<String> list = new ArrayList<String>();
        list.add("Dave");
        list.add(null);
        System.out.println("list = "+list);

        try
        {
            String str = list.get(index);
            System.out.println("str = " + str);
            System.out.println("length = " + str.length());
        }
        catch (IndexOutOfBoundsException indexOutOfBoundsException)
        {
            System.out.println("Enter the index value "
                    + "less than or equal to 1.");
        }
        catch (NullPointerException nullPointerException)
        {
            System.out.println("String value is null,so "
                    + "cannot calculate the length.");
        }
        
        System.out.println("Normal flow..");

    }
}
Output
Enter the index : 8
list = [Dave, null]
Enter the index value less than or equal to 1.
Normal flow..

----------------------------------------------

Enter the index : 1
list = [Dave, null]
str = null
String value is null,so cannot calculate the length.
Normal flow..
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ExceptionDemo_multiple_catch-blocks_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_multiple_catch-blocks_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/a4490537e1a166e28ac44e3caed727d335adc51d/BasicJava/ExceptionDemo_multiple_catch-blocks_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