Difference between revisions of "Java/C3/Exception-Handling/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 20: Line 20:
 
** '''try-catch''' '''block'''
 
** '''try-catch''' '''block'''
 
** '''finally block '''
 
** '''finally block '''
 
 
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| In this tutorial we will learn about:
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| In this tutorial we will learn about:
  
Line 29: Line 27:
 
** '''try-catch''' '''block '''and
 
** '''try-catch''' '''block '''and
 
** '''finally block '''
 
** '''finally block '''
 
 
  
 
|-
 
|-
Line 40: Line 36:
 
* '''JDK 1''' .8
 
* '''JDK 1''' .8
 
* '''Eclipse''' 4.3.1
 
* '''Eclipse''' 4.3.1
 
 
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Here we are using
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Here we are using
  
Line 47: Line 41:
 
* '''JDK 1 .8 '''and
 
* '''JDK 1 .8 '''and
 
* '''Eclipse 4.3.1'''
 
* '''Eclipse 4.3.1'''
 
 
  
 
|-
 
|-
Line 57: Line 49:
 
* Basic knowledge of '''Java''' and '''Eclipse IDE'''
 
* Basic knowledge of '''Java''' and '''Eclipse IDE'''
 
* For relevant tutorials, please visit http://www.spoken-tutorial.org
 
* For relevant tutorials, please visit http://www.spoken-tutorial.org
 
 
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
  
Line 73: Line 63:
 
* '''unchecked''' exceptions and  
 
* '''unchecked''' exceptions and  
 
* '''checked''' exceptions.
 
* '''checked''' exceptions.
 
  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| An '''exception''' is an unexpected event, which occurs during the execution of a program.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| An '''exception''' is an unexpected event, which occurs during the execution of a program.
  
  
It interrupts the normal flow of the  
+
It interrupts the normal flow of the program and results in an abnormal termination.  
 
+
program and results in an abnormal termination.  
+
  
  
 
Based on their occurrence, '''exceptions''' are classified as  
 
Based on their occurrence, '''exceptions''' are classified as  
 
 
* '''unchecked''' exceptions and  
 
* '''unchecked''' exceptions and  
 
* '''checked''' exceptions.  
 
* '''checked''' exceptions.  
 
 
  
 
|-
 
|-
Line 118: Line 102:
  
 
}}
 
}}
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now type the following code to represent the '''Marks '''class.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now type the following code to represent the '''Marks class.'''
  
  
Line 130: Line 114:
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us run this program and verify the output.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us run this program and verify the output.
  
We can see that the values in the array are getting printed.
+
We can see that the values in the '''array''' are getting printed.
 +
 
 +
|-
 +
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"|
 +
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us check what will happen if we are trying to access an array element which is not existing.
  
 
|-
 
|-
Line 138: Line 126:
  
 
Highlight the index 50  
 
Highlight the index 50  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us check what will happen if we are trying to access an array element which is not existing.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"|Now type the following code.
  
Now type the following code.
+
We know that there are only 5 elements in our '''array'''.
  
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.
 
+
But in this statement we are trying to access the element at index 50 which is not existing.
+
  
 
|-
 
|-
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Click on run icon
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Click on '''run''' icon
  
  
Line 158: Line 144:
  
 
Highlight the print statement
 
Highlight the print statement
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us run this program now.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us '''run''' this program now.
  
  
Line 167: Line 153:
  
 
The error message indicates the details of the exception like
 
The error message indicates the details of the exception like
 
 
* type of '''exception'''
 
* type of '''exception'''
* where it occurred and other details.
+
* where it occurred  
 +
* and other details.
  
 
    
 
    
  
Note that the print statement is not executed as the program terminates after the error.
+
Note that the '''print statement''' is not executed as the program terminates after the error.
  
 
    
 
    
  
This is an example of '''Unchecked''' exception.
+
This is an example of '''Unchecked exception.'''
 
+
 
+
 
+
  
 
|-
 
|-
Line 188: Line 171:
  
 
* '''Unchecked Exceptions''' are called as '''Runtime Exceptions'''  
 
* '''Unchecked Exceptions''' are called as '''Runtime Exceptions'''  
 
 
* They handle the programming bugs and logical errors such as  
 
* They handle the programming bugs and logical errors such as  
 
*
 
 
** Dividing a number by zero  
 
** Dividing a number by zero  
 
** Accessing an array element which is not existing  
 
** Accessing an array element which is not existing  
 
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"|  
 
+
* '''Unchecked exceptions''' are called as '''Runtime exception''' as it is checked only during the execution.
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| * '''Unchecked exceptions''' are called as '''Runtime exception''' as it is checked only during the execution.
+
 
+
 
* They handle the programming bugs and logical errors such as  
 
* They handle the programming bugs and logical errors such as  
 
*
 
 
** Dividing a number by zero and
 
** Dividing a number by zero and
 
** Accessing an array element which is not existing  
 
** Accessing an array element which is not existing  
 
 
  
 
|-
 
|-
Line 228: Line 202:
  
 
Inside the '''catch block''' we can write the code for  
 
Inside the '''catch block''' we can write the code for  
 
 
* displaying error messages or
 
* displaying error messages or
 
* recovering from the error
 
* recovering from the error
 
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Switch to eclipse
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Switch to eclipse
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now let us switch to eclipse.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now let us switch to '''eclipse'''.
  
 
|-
 
|-
Line 264: Line 235:
  
 
Highlight the Exception object e
 
Highlight the Exception object e
 
 
 
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now we have to add a corresponding '''catch block.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now we have to add a corresponding '''catch block.'''
  
Line 273: Line 241:
  
  
Here we are printing a custom message “Array Overflow Exception occurred”
+
Here we are printing a custom message '''“Array Overflow Exception occurred”'''
  
  
Line 292: Line 260:
  
  
But this time, note that printing the marks array is also executed.
+
But this time, note that printing the '''marks array''' is also executed.
  
  
In this way we can handle exceptions.
+
In this way we can handle '''exceptions'''.
  
 
|-
 
|-
Line 301: Line 269:
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Next let us see how to use multiple '''catch blocks.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Next let us see how to use multiple '''catch blocks.'''
  
We can use them''' '''when different types of '''exceptions''' are raised by a block.
+
We can use them when different types of '''exceptions''' are raised by a '''block'''.
  
 
|-
 
|-
Line 314: Line 282:
  
  
This line of code divides an array element by zero as the value of '''a '''is '''zero'''.
+
This line of code divides an '''array element''' by '''zero''' as the value of '''a '''is '''zero'''.
  
  
Line 329: Line 297:
  
 
}
 
}
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us now add one more catch block to handle the '''ArithmeticException.'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us now add one more '''catch block''' to handle the '''ArithmeticException.'''
  
  
 
So type the following code after the existing '''catch block'''.
 
So type the following code after the existing '''catch block'''.
 
 
 
  
 
|-
 
|-
Line 345: Line 310:
  
  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us run the program again.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us '''run''' the program again.
  
  
This time the error message "Arithmetic Exception occurred" gets printed as it is caught first.
+
This time the error message '''"Arithmetic Exception occurred"''' gets printed as it is caught first.
  
  
Line 370: Line 335:
  
  
'''Checked''' '''exceptions''' are checked at compile time.
+
'''Checked exceptions''' are checked at '''compile time'''.
  
  
Line 380: Line 345:
 
* Accessing a '''file''' which is not existing or
 
* Accessing a '''file''' which is not existing or
 
* Accessing a '''network system''' when the network is down  
 
* Accessing a '''network system''' when the network is down  
 
 
  
 
|-
 
|-
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| switch to Eclipse, create a new '''class MarksFile'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Switch to Eclipse, create a new '''class MarksFile'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now let us switch to '''Eclipse''' and create a new '''class MarksFile. '''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now let us switch to '''Eclipse''' and create a new '''class MarksFile. '''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Type main and press control+space
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Type main and press control+space
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us add '''main''' method.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us add '''main method'''.
  
 
|-
 
|-
Line 404: Line 367:
  
  
So type the following code'''.'''
+
So type the following code.
  
  
Line 410: Line 373:
  
  
'''FileReade'''r object can be used to access and read a particular file.
+
'''FileReader''' object can be used to access and read a particular file.
  
 
|-
 
|-
Line 430: Line 393:
  
 
We will learn about '''package''' and its usage in detail in a later tutorial.
 
We will learn about '''package''' and its usage in detail in a later tutorial.
 
 
 
  
 
|-
 
|-
Line 444: Line 404:
  
 
Point the error
 
Point the error
 
 
 
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| To allow '''fr '''to access a file called''' Marks''' which is located in the home folder, type the following code.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| To allow '''fr '''to access a file called''' Marks''' which is located in the home folder, type the following code.
  
Line 454: Line 411:
  
 
Now an error comes up. It indicates that this line of code can create a '''FileNotFoundException.'''
 
Now an error comes up. It indicates that this line of code can create a '''FileNotFoundException.'''
 
 
 
  
 
|-
 
|-
Line 466: Line 420:
  
  
We can see that eclipse automatically inserts the '''try catch block''' to rectify this error.
+
We can see that '''Eclipse''' automatically inserts the '''try catch block''' to rectify this error.
  
  
Line 489: Line 443:
  
  
The code inside this block is executed whether exception has occurred or not.
+
The code inside this '''block''' is executed whether exception has occurred or not.
  
  
It contains a print statement.
+
It contains a '''print statement'''.
  
 
|-
 
|-
Line 502: Line 456:
  
  
So type, fr dot close
+
So type, '''fr dot close'''
 
+
 
+
 
+
  
 
|-
 
|-
Line 512: Line 463:
  
 
Click on the error and double click '''Surround with try/catch '''
 
Click on the error and double click '''Surround with try/catch '''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now eclipse indicates that this will raise an '''IOException.'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now '''Eclipse''' indicates that this will raise an '''IOException.'''
  
  
Line 519: Line 470:
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Click on '''run''' icon
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Click on '''run''' icon
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now let us run the program.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Now let us '''run''' the program.
  
 
|-
 
|-
Line 540: Line 491:
  
  
But we can see that the print statement inside '''finally block''' gets executed.
+
But we can see that the '''print statement''' inside '''finally block''' gets executed.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| So click on '''Files''' in the '''Ubuntu desktop''' and go inside the''' home folder'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| So click on '''Files''' in the '''Ubuntu desktop''' and go inside the''' home folder'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us now create a text file '''Marks''' in our home folder.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| Let us now create a text file '''Marks''' in our home folder.
 
 
 
  
 
|-
 
|-
Line 557: Line 505:
  
 
For example it can be specified as '''D:\\Marks.txt'''
 
For example it can be specified as '''D:\\Marks.txt'''
 
 
 
  
 
|-
 
|-
Line 575: Line 520:
  
  
The cleanup operation i.e closing the '''FileReader''' Object '''fr '''is also executed successfully.
+
The '''cleanup operation''' i.e closing the '''FileReader''' Object '''fr '''is also executed successfully.
  
 
|-
 
|-
Line 594: Line 539:
 
** '''try-catch''' '''block'''
 
** '''try-catch''' '''block'''
 
** '''finally block '''
 
** '''finally block '''
 
  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| In this tutorial we have learnt about
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| In this tutorial we have learnt about
Line 603: Line 547:
 
** '''try-catch''' '''block '''and
 
** '''try-catch''' '''block '''and
 
** '''finally block '''
 
** '''finally block '''
 
 
  
 
|-
 
|-
Line 614: Line 556:
  
  
Refer to the java program named '''Demo.java '''provided in the assignment section.
+
Refer to the '''Java''' program named '''Demo.java '''provided in the assignment section.
  
  
An exception will be raised when you run this code.
+
An '''exception''' will be raised when you run this code.
  
  
Identify the code which is responsible for the exception.  
+
Identify the code which is responsible for the '''exception'''.  
  
  
 
Rectify it using a''' try-catch block'''
 
Rectify it using a''' try-catch block'''
 
 
 
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| As an assignment,
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.155cm;padding-right:0.191cm;"| As an assignment,
  
Line 633: Line 572:
  
  
Refer to the Java program named '''Demo.java '''provided in the '''Assignment''' link of this tutorial.
+
Refer to the '''Java''' program named '''Demo.java '''provided in the '''Assignment''' link of this tutorial.
  
  
An exception will be raised when you run this code.
+
An '''exception''' will be raised when you run this code.
  
  
Identify the code which is responsible for the exception.  
+
Identify the code which is responsible for the '''exception'''.  
  
  
 
Rectify it using a''' try-catch block.'''
 
Rectify it using a''' try-catch block.'''
 
 
 
  
 
|-
 
|-
Line 663: Line 599:
  
 
• Conducts workshops using spoken tutorials
 
• Conducts workshops using spoken tutorials
 
 
• Gives certificates on passing the online tests
 
• Gives certificates on passing the online tests
  

Latest revision as of 15:15, 9 May 2017

Visual Cue
Narration
Slide 1

Exception Handling

Welcome to the spoken tutorial on Exception Handling.
Slide 2

Learning Objectives

  • What is an Exception
  • Checked and Unchecked Exceptions
  • Handling the Exceptions using
    • try-catch block
    • finally block
In this tutorial we will learn about:
  • What is an exception
  • Checked and unchecked exceptions
  • Handling the exceptions using
    • try-catch block and
    • finally block
Slide 3

Software Requirements

  • Ubuntu 16.04 OS
  • JDK 1 .8
  • Eclipse 4.3.1
Here we are using
  • Ubuntu Linux 16.04 OS
  • JDK 1 .8 and
  • Eclipse 4.3.1
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
  • It interrupts the normal flow and results in an abnormal termination.
  • Exceptions are classified as
  • unchecked exceptions and
  • checked exceptions.
An exception is an unexpected event, which occurs during the execution of a program.


It interrupts the normal flow of the program and results in an abnormal termination.


Based on their occurrence, exceptions are classified as

  • unchecked exceptions and
  • checked exceptions.
In Eclipse IDE, create a project called ExceptionDemo Now we will open eclipse and create a new project called ExceptionDemo.


Inside this project we will create the necessary classes to demonstrate exception handling.

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;


System.out.println("Mark List");

for(int i=0;i<5;i++){

System.out.println(marks[i]);

}}

Now type the following code to represent the Marks class.


This program prints the marks of 5 students that is stored in an array marks.

Click on run icon


Highlight the output

Let us run this program and verify the output.

We can see that the values in the array are getting printed.

Let us check what will happen if we are trying to access an array element which is not existing.
Type the following code (before printing the array)

System.out.println("\t"+marks[50]);

Highlight the index 50

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


Highlight the error Message

ArrayIndexOutOfBoundsException and

at Marks.main(Marks.java:7)


Highlight the print statement

Let us run this program now.


We can see that the program terminates with an error message:

ArrayIndexOutOfBoundsException “at line number 7.


The error message indicates the details of the exception like

  • type of exception
  • where it occurred
  • and other details.


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 Exceptions
  • They handle the programming bugs and logical errors such as
    • Dividing a number by zero
    • Accessing an array element which is not existing
  • Unchecked exceptions are called as Runtime exception as it is checked only during the execution.
  • They handle the programming bugs and logical errors such as
    • Dividing a number by zero and
    • Accessing an array element which is not existing
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 Exception e

Highlight the portion of code inside catch block

This portion of the code within a try block, can possibly raise an exception.


The corresponding catch block can receive the exception details in object e.


Inside the catch block we can write the code for

  • displaying error messages or
  • recovering from the error
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");

}


Highlight "Array Overflow Exception occurred"


Highlight the Exception object e

Now we have to add a corresponding catch block.


So type the following code.


Here we are printing a custom message “Array Overflow Exception occurred”


Inside the round brackets we have created an instance of ArrayIndexOutOfBoundsException.


So this block can catch exceptions of type ArrayIndexOutOfBoundsException.

Click on run icon


Highlight the output ie Printing the array

Now let us run the program.


We can see that the error message gets printed.


But this time, note that printing the marks array is also executed.


In this way we can handle exceptions.

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]);)


System.out.println(marks[2]/a);


Highlight the above line of code

Type the following code inside the try block.


This line of code divides an array element by zero as the value of a is zero.


So an ArithmeticException will be raised first.

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.


So type the following code after the existing catch block.

Click on run icon


Highlight the error message in the output


Let us run the program again.


This time the error message "Arithmetic Exception occurred" gets printed as it is caught first.


The remaining portion of the code outside the try catch block executes.

Slide 9

Checked Exceptions

  • They are checked at compile time.
  • They must be handled before running the program

Example:

  • Accessing a File which is not existing
  • Accessing a Network System when the network is down


Next let us see about checked exceptions.


Checked exceptions are checked at compile time.


So they must be handled before running the program.


For example:

  • Accessing a file which is not existing or
  • Accessing a network system when the network is down
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;


Highlight FileReader fr=null;


Highlight FileReader

Now we want to read a file located in the computer.


So type the following code.


Here the FileReader object fr is initialized as null.


FileReader object can be used to access and read a particular file.

Point the error


Click on the error and import the class


Go to the top of the code file and highlight the import statement

Eclipse will show an error.


To rectify the error, click on it and double click import FileReader java dot io.


The FileReader class is imported from the java dot io package.


We will learn about package and its usage in detail in a later tutorial.

Type the following code

fr = new FileReader("/home/spoken/Marks");


Highlight "/home/spoken/Marks


Point the error

To allow fr to access a file called Marks which is located in the home folder, type the following code.


The path shown here is to be replaced with that of your system's home folder.


Now an error comes up. It indicates that this line of code can create a FileNotFoundException.

Click on the error and double click Surround with a try catch block.


Click on the error and double click Surround with try/catch.


We can see that Eclipse automatically inserts the try catch block to rectify this error.


So we can understand that this is a checked exception.

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.


finally block usually follows a try-catch block .


The code inside this block is executed whether exception has occurred or not.


It contains a print statement.

Type fr.close();


Now let us close the file reference inside the finally block.


So type, fr dot close

Point the error


Click on the error and double click Surround with try/catch

Now Eclipse indicates that this will raise an IOException.


So click on the error and double click Surround with try/catch.

Click on run icon Now let us run the program.
Highlight FileNotFound Exception


Highlight NullPointerException


Highlight the print statement

Inside finally block

We can see that FileNotFoundException message is printed.


This is because we don't have a file named Marks in our home folder.


We can also see a NullPointerException as fr still refers to a null value.


But we can see that the print statement inside finally block gets executed.

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.


For example it can be specified as D:\\Marks.txt

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.


And “Inside finally block” gets printed.


The cleanup operation i.e closing the FileReader Object fr is also executed successfully.

With this we come to the end of this tutorial.


Let us summarize.

Slide 10

Summary

  • What is an Exception
  • Checked and Unchecked Exceptions
  • Handling the Exceptions using
    • try-catch block
    • finally block
In this tutorial we have learnt about
  • What is an exception
  • Checked and unchecked exceptions
  • Handling the exceptions using the
    • try-catch block and
    • finally block
Slide 10

Assignment

Learn about another Runtime Exception called NullPointerException


Refer to the Java program named Demo.java provided in the assignment section.


An exception will be raised when you run this code.


Identify the code which is responsible for the exception.


Rectify it using a try-catch block

As an assignment,


Learn about another Runtime Exception called NullPointerException.


Refer to the Java program named Demo.java provided in the Assignment link of this tutorial.


An exception will be raised when you run this code.


Identify the code which is responsible for the exception.


Rectify it using a try-catch block.

About Project

(retain the slide as in TEX file)

The video at the following link summarizes the Spoken Tutorial Project.


Please download and watch it.

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


For more details, please write to us.

About NMEICT

(retain the slide as in TEX file)

Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.


More information on this Mission is available at the link shown.

Contributor slide

(retain the slide as in TEX file)

This script has been contributed by:

Dept. of Information

Technology, Amal Jyothi College of Engineering


This is Priya from IIT Bombay. Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst