Java/C3/Exception-Handling/English
|
|
Slide 1
Exception Handling |
Welcome to the spoken tutorial on Exception Handling. |
Slide 2
Learning Objectives
|
In this tutorial we will learn about:
|
Slide 3
Software Requirements
|
Here we are using
|
Slide 4
Prerequisites
|
To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
If not, for relevant Java tutorials, please visit the link shown. |
Slide 5(a)
Exceptions
|
An exception is an unexpected event, which occurs during the execution of a program.
program and results in an abnormal termination.
|
In Eclipse IDE, create a project called ExceptionDemo | Now we will open eclipse and create a new project called ExceptionDemo.
|
Right click on src folder and right-click New-> Class and type the class name as Marks and hit Enter | We will create a new class Marks. |
//Copy and paste the code.
public static void main(String[] args) { int[] marks={30,40,35,34,45}; int a=0;
for(int i=0;i<5;i++){ System.out.println(marks[i]); }} |
Now type the following code to represent the Marks class.
|
Click on run icon
|
Let us run this program and verify the output.
We can see that the values in the array are getting printed. |
Type the following code (before printing the array)
System.out.println("\t"+marks[50]); Highlight the index 50 |
Let us check what will happen if we are trying to access an array element which is not existing.
Now type the following code. We know that there are only 5 elements in our array. But in this statement we are trying to access the element at index 50 which is not existing. |
Click on run icon
ArrayIndexOutOfBoundsException and at Marks.main(Marks.java:7)
|
Let us run this program now.
“ArrayIndexOutOfBoundsException “at line number 7.
Note that the print statement is not executed as the program terminates after the error.
This is an example of Unchecked exception.
|
Slide 7
Unchecked Exception
|
* Unchecked exceptions are called as Runtime exception as it is checked only during the execution.
|
Now let us learn about how to handle an exception using try catch block. | |
Slide 8
Handling Exceptions Highlight the portion of the code which can raise the exception
Highlight the portion of code inside catch block |
This portion of the code within a try block, can possibly raise an exception.
|
Switch to eclipse | Now let us switch to eclipse. |
Type the code to add try block
try{ System.out.println("\t"+marks[50]); } |
First let us add a try block around the code which caused the exception like this. |
Copy the following code
catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array Overflow Exception occurred"); }
|
Now we have to add a corresponding catch block.
|
Click on run icon
|
Now let us run the program.
|
Next let us see how to use multiple catch blocks.
We can use them when different types of exceptions are raised by a block. | |
Insert the following code inside the try block (before the line of code System.out.println("\t"+marks[50]);)
|
Type the following code inside the try block.
|
Type the following code
catch(ArithmeticException e) { System.out.println("Arithmetic Exception occurred"); } |
Let us now add one more catch block to handle the ArithmeticException.
|
Click on run icon
|
Let us run the program again.
|
Slide 9
Checked Exceptions
Example:
|
Next let us see about checked exceptions.
|
switch to Eclipse, create a new class MarksFile | Now let us switch to Eclipse and create a new class MarksFile. |
Type main and press control+space | Let us add main method. |
Type the following code
FileReader fr=null;
|
Now we want to read a file located in the computer.
|
Point the error
|
Eclipse will show an error.
|
Type the following code
fr = new FileReader("/home/spoken/Marks");
|
To allow fr to access a file called Marks which is located in the home folder, type the following code.
|
Click on the error and double click Surround with a try catch block.
|
Click on the error and double click Surround with try/catch.
|
Go to the MarksFile class | Next let us see how to use finally block. |
Copy the following code
finally{ System.out.println("Inside finally block"); } |
Type the following code.
|
Type fr.close();
|
Now let us close the file reference inside the finally block.
|
Point the error
|
Now eclipse indicates that this will raise an IOException.
|
Click on run icon | Now let us run the program. |
Highlight FileNotFound Exception
Inside finally block |
We can see that FileNotFoundException message is printed.
|
So click on Files in the Ubuntu desktop and go inside the home folder | Let us now create a text file Marks in our home folder.
|
In the eclipse show the path
D:\\Marks.txt |
If you are a windows user, create a text file in your local drive and mention its path.
|
Click on run icon | Let us now run the program again. |
Highlight the output
Inside finally block |
We can verify that there are no exceptions once the Marks file is created.
|
With this we come to the end of this tutorial.
| |
Slide 10
Summary
|
In this tutorial we have learnt about
|
Slide 10
Assignment Learn about another Runtime Exception called NullPointerException
|
As an assignment,
|
About Project
(retain the slide as in TEX file) |
The video at the following link summarizes the Spoken Tutorial Project.
|
About Workshops
(retain the slide as in TEX file) |
The Spoken Tutorial Project Team
• Conducts workshops using spoken tutorials • Gives certificates on passing the online tests
|
About NMEICT
(retain the slide as in TEX file) |
Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
|
Contributor slide
(retain the slide as in TEX file) |
This script has been contributed by:
Dept. of Information Technology, Amal Jyothi College of Engineering
|