Home | | Sitemap ||Page number :20

After looking at the code and the output of the program, some conclusions can be drawn

  1. Program does not terminate in an exceptional condition if we use “try-catch” statements. This is proved by the fact that the line System.out.println("EXCEPTION DETAIL PRINTED");was executed after the exception occurred.
  2. We can print the details of exception by simply printing the exception’s object i.e. is “e”.

Let us imagine that we are writing a program which manipulates digits in arrays (You will be learning about arrays in the last chapter). We suspect that there might be a condition where our program can generate. (i). ArithmeticException (ii). ArrayIndexOutOfBoundsException How will be the two exceptional cases handled simultaneously? The answer to this problem is multiple catch blocks.

Syntax-try { // Block of code to be inspected for errors // Statements } catch(Exception1 obj) { // Handling exceptions // All statements } catch(Exception2 obj) { // Handling exceptions // All statements }

throw and throws keywords---

throw-

Till now we have been handling exceptions automatically thrown by java runtime system due to certain lines in our code. The “throw” keyword is used to manually throw an exception. As soon as the interpreter encounters the line with the throw keywords, it searches for the catch statement of the try block in which the “throw” keyword is enclosed. If no catch block is found then the default exception handler handles the exception and terminates the program.

class thrw { public static void main(String args[]) { try { System.out.println("Try block's execution begins"); throw new ArithmeticException (); } catch(ArithmeticException obj) { System.out.println("Inside the catch block"); System.out.println("Exception detail---"+obj); } } }

We have created an object of ArithmeticException using new operator. The catch block handles the exception.

tr

throws

If we construct a method that throws an exception which it does not handle, the keyword “throws” warns the calling method of the exception. return_type method( parameter list) throws exception { // Method’s body // Other statements }

A straightforward and clear example will be a simple program that takes some input from the user.

import java.io.*; class trws { public static void main(String args[]) throws IOException /. Use of “throws” keyword { int no;

InputStreamReader rd=new InputStreamReader(System.in); BufferedReader inp=new BufferedReader(rd);

System.out.println("Enter a number"); String v1=inp.readLine(); no=Integer.parseInt(v1);

System.out.println("Multiplying a number"); no=no*no; System.out.println("Square of the no. is "+no); } }

ryrty

Exercise

Q1. What is an exception?

Q2. Why is exception handling a vital aspect of successful programming?

Q3. Explain the use of try-catch statements.

Q4. Which two other keywords other than ‘try-catch’ are used to handle exceptions?

Q5. When do these exceptional conditions occur?

(i). ArithmeticException (ii). ArrayIndexOutOfBoundsException