You are on page 1of 43

Programming Language (JAVA)

Unit 6.10 Exception Handling

Presentation 2

Objectives

At the end of this presentation, you will be able to : Demonstrate the use of try, catch, finally blocks List the built in exceptions Create user defined exception

try block
The exceptions in a program are caught or trapped using a try block.
A block of code which may generate an exception to terminate the program should be placed inside the try block.

try block
Syntax

try { statements ;// code which generates exception }

try block - Example


try { int A,B,C; A=Integer.parseInt(args[0]); B=Integer.parseInt(args[1]); C=A/B; System.out.println(The value of C:- + C); }

catch block
A try block should have at least one catch block.
The try block may or may not generate an exception. The catch block is responsible for catching the exception thrown from the try block. Hence, a catch block is placed immediately after a try block.

catch block
Syntax

catch(Exceptiontype object) { statements;// code which handles exception }

catch block
Example

catch(ArithmeticException exp) { System.out.println(I have caught the exception); }

Exception Handling Mechanism

Hands-On!
Program excep1.java uses try and catch blocks to handle the generated exception ArithmeticException

Hands-On!
Program ArrayExcepDemo.java generates ArrayIndexOutOfBoundsException.

Activity 6.10.3
Fill in the blanks: Step 1: Open the excep3.java file.
1. class excep3

2. {
3. 4. 5. 6. public static void main(_______ args[]) { int a=10,b=5,c=5,x=0; _____

Activity 6.10.3 (Contd..)


Fill in the blanks:
7. 8. 9. 10. 11.

{ x = a/(b-c); } catch(___________________ ae) {

Activity 6.10.3 (Contd..)


Fill in the blanks:
12. 13. 14. 15.

System.out.println(The exception is caught); } System.out.println(The value of x is :- +x); }

16. } Step 2: Read the program and fill in the blanks with appropriate code. Step 3: Save the program. Step 4: Compile and execute it.

Activity 6.10.4
1. Give the output for program excep4.java.
Step 1: Open excep4.java file. Step 2: Read the program, predict and write the output.

Lab Exercise
1.Type the code of program error2.java.

class error2 { public static void main(String args[]) { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); int c=Integer.parseInt(args[2]);

Lab Exercise (Contd..)


int d=Integer.parseInt(args[3]); double Result= a+b*c/d; System.out.println(The value of the expression is + Result); } }

Lab Exercise (Contd..)


1. Compile and execute the program error2.java
2. Enter positive values for a, b, c and d. 3. See the result.

4. Execute the program again and enter the value of d as 0. An arithmetic exception occurs.
5. Add try and catch blocks to handle this exception. 6. Compile, execute the program and see the result.

Multiple Catch
Some times a code can generate more than one type of exception.
If more than one type of exception arises, more than one catch statement should be used to handle those exception types.

Hands-On!
Program MultiCatchDemo.java illustrates the use of multi catch statement.

Hands-On!
Program ExcepClassDemo.java illustrates the use of Exception class to catch any type of exception generated by the program.

Lab Exercise
2. Write a program to find the sum of 10 numbers in an array. Use try, catch block to handle ArrayIndexOutOfBoundsException.

throw statement
The throw statement allows you to throw exceptions that are not thrown by Java runtime system.
When throw is used inside the program, it implies that the program is going to throw an exception.

throw statement
Syntax

throw new exceptiontype ();

Hands-On!
Program throwdemo.java illustrates the use of throw statement.

Activity 6.10.5
Fill in the blanks to complete the program that throws exception explicitly:
Step 1: Open the excep5.java student data file. Step 2: Read the program and fill in the blanks with appropriate code. 1. _______ excep5
2. {

3.
4. 5.

public static void main(String args[])


{ int a=5,b=10,c=0;

Activity 6.10.5 (Contd..)


6.
c = b/a; 7. try 8. { 9. ________ new Exception(What will happen); 10. } 11. catch(____________ ae) 12. { 13. System.out.println(The exception caught); 14. } 15. System.out.println(The value of c is :- +c); 16. } 17. } Step 3: Save the program. Step 4: Compile and execute it.

finally
The finally block contains statements for doing the final process such as de-allocation of memory etc.
It may be added immediately after the try block or after the last catch block. A try block should have at least one catch block or finally block immediately following it.

finally
Syntax

finally { statements ;// code to be executed }

Hands-On!
Program finallydemo.java demonstrates the use of finally block.

Activity 6.10.6
Detect the errors in the following program:
Step 1: Open excep6.java student data file.

1. class excep6 2. { 3. public static void main(String args[]) 4. { 5. int a=0,b=5,c=0; 6. try 7. {

Activity 6.10.6 (Contd..)


8.
9. 10. 11. 12.

c=b/a;
} catch(ArithemticException e) { System.out.println("Iam from catch block");

13.

Activity 6.10.6 (Contd..)


14. final
15. {

16. System.out.println("I am from finally block"); 17. 18. } }

19. }
Step 2:Read the program and detect the errors in program.

Activity 6.10.7
Detect the errors in the following program:
Step 1: Open excep7.java student data file.

class excep7 { public static void main(String args[]) { int a[]={1,2}; try {

Activity 6.10.7 (Contd..)


if (a[1]>a[2])
System.out.println(a[1]); Else

System.out.println(a[2]);
} catch(ArrayIndexOutOfBoundsException e) {

Activity 6.10.7 (Contd..)


System.out.println("I am from catch block"); } finally { System.out.println("I am from finally block"); } } } Step 2: Read the program and correct the mistakes. Step 3: Save, compile and execute the program.

Lab Exercise
3. Write a program to get 5 values of array elements from the command line and print those values in the main() method. Add try catch blocks to handle ArrayIndexOutOfBoundsException
4. Write a program to generate two exceptions and catch using multiple catch blocks. Add a finally block that prints The program is over.

User-Defined Exceptions
The built-in exceptions handle most of the common errors.
Certain application specific exceptions may not be covered in the built-in exceptions. Create a user-defined exception to handle the situation. Use exception handling mechanism to avoid the abnormal program termination.

User-Defined Exceptions

Hands-On!
Program myexception.java illustrates the creation of User-Defined Exception.

Lab Exercise
5. Write a program to generate an exception by your own (user defined exception) and handle the exception.

Summary
In this presentation, you learnt the following: finally block is used to handle any exception generated by try block, even if the catch block is not able handle the exception. The finally block contains statements for doing the final process such as de-allocation of memory etc. The class Exception is the subclass of the class Throwable.

Assignment

1. Define exception.
2. Write the syntax and example for a try block.

3. Is it essential to capture all types of exceptions?


4. What is the use of finally block?

You might also like