Professional Documents
Culture Documents
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
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 block
Example
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; _____
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]);
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
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.
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
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. {
c=b/a;
} catch(ArithemticException e) { System.out.println("Iam from catch block");
13.
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 {
System.out.println(a[2]);
} catch(ArrayIndexOutOfBoundsException e) {
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.