Difference between revisions of "Java/C3/Exception-Handling/English"
Nancyvarkey (Talk | contribs) |
|||
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 ''' | + | | 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;"| | + | | 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. |
− | + | 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''' | + | 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 | + | 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 | + | '''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;"| | + | | 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''' | + | | 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: | ||
− | ''' | + | '''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 | + | 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 | + | | 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 | + | 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
|
|
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.
|
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. |
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
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
|
|
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
|