1 try – multiple catch handling

1 try – multiple catch handling is maximum use program in java. In this example we will work on ArrayIndexOutOfBoundsException, ArithmeticException and normal Exception.

Here we create one try block with three catch block with three different run time errors.

 

Let’s see 1 try – multiple catch program with videos

 

class Exc4

{

            public static void main(String args[])

            {

                        int x[] = new int[2];

 

                        try

                        {

                                    x[0] = 23;

                                    x[1] = 0;

                                    //x[2] = 12;      // ArrayIndexOutBoundsException

 

                                    int div = x[0]/x[1];      // ArithmeticException

                                    System.out.println(“Div = ” + div);

                        }

                        catch(ArrayIndexOutOfBoundsException ex)

                        {

                                    System.out.println(ex);

                        }          

                        catch(ArithmeticException ex)

                        {

 

                                    System.out.println(ex);

                        }

                        catch(Exception ex)

                        {

                                    System.out.println(ex);

                        }

            }

}

Output:

Output -1

If we use x[2] = 12 line in program then we found ArrayIndexOutOfBoundsException

 

Output -2

If we do not use x[2] = 12 line in program then we found ArithmeticException.

 

 

 

Video available on YouTube now.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


Earn money online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

353total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint