Java/C3/Exception-Handling/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on Exception Handling.
00:06 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 .
00:20 Here we are using Ubuntu Linux 16.04 OS JDK 1 .8 and Eclipse 4.3.1
00:32 To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
00:39 If not, for relevant Java tutorials, please visit the link shown.
00:45 An exception is an unexpected event, which occurs during the execution of a program.
00:52 It interrupts the normal flow of the program and results in an abnormal termination.
01:00 Based on their occurrence, exceptions are classified as unchecked exceptions and checked exceptions.
01:08 Now we will open eclipse and create a new project called ExceptionDemo.
01:16 Inside this project we will create the necessary classes to demonstrate exception handling.
01:24 We will create a new class Marks.
01:28 Now type the following code to represent the Marks class.
01:34 This program prints the marks of 5 students that is stored in an array marks.
01:41 Let us run this program and verify the output.
01:45 We can see that the values in the array are getting printed.
01:50 Let us check what will happen if we are trying to access an array element which is not existing.
01:57 Now type the following code.
02:00 We know that there are only 5 elements in our array.
02:04 But in this statement we are trying to access the element at index 50 which is not existing.
02:12 Let us run this program now.
02:15 We can see that the program terminates with an error message:“ArrayIndexOutOfBoundsException “at line number 7.
02:25 The error message indicates the details of the exception like type of exception

where it occurred and other details.

02:35 Note that the print statement is not executed as the program terminates after the error.
02:42 This is an example of Unchecked exception.
02:46 Unchecked exceptions are called as Runtime exception as it is checked only during the execution.
02:54 They handle the programming bugs and logical errors such as Dividing a number by zero and Accessing an array element which is not existing .
03:07 Now let us learn about how to handle an exception using try catch block.
03:13 This portion of the code within a try block, can possibly raise an exception.
03:19 The corresponding catch block can receive the exception details in object e.
03:26 Inside the catch block we can write the code for displaying error messages or recovering from the error.
03:34 Now let us switch to eclipse.
03:37 First let us add a try block around the code which caused the exception like this.
03:44 Now we have to add a corresponding catch block.
03:48 So type the following code.
03:51 Here we are printing a custom message “Array Overflow Exception occurred”
03:57 Inside the round brackets we have created an instance of ArrayIndexOutOfBoundsException.
04:05 So this block can catch exceptions of type ArrayIndexOutOfBoundsException.
04:11 Now let us run the program.
04:14 We can see that the error message gets printed.
04:18 But this time, note that printing the marks array is also executed.
04:24 In this way we can handle exceptions.
04:27 Next let us see how to use multiple catch blocks.
04:32 We can use them when different types of exceptions are raised by a block.
04:38 Type the following code inside the try block.
04:42 This line of code divides an array element by zero as the value of a is zero.
04:49 So an ArithmeticException will be raised first.
04:53 Let us now add one more catch block to handle the ArithmeticException.
04:58 So type the following code after the existing catch block.
05:03 Let us run the program again.
05:06 This time the error message "Arithmetic Exception occurred" gets printed as it is caught first.
05:13 The remaining portion of the code outside the try catch block executes.
05:19 Next let us see about checked exceptions.
05:23 Checked exceptions are checked at compile time.
05:27 So they must be handled before running the program.
05:31 For example: Accessing a file which is not existing or Accessing a network system when the network is down .
05:41 Now let us switch to Eclipse and create a new class MarksFile.
05:47 Let us add main method.
05:50 Now we want to read a file located in the computer.
05:54 So type the following code.
05:57 Here the FileReader object fr is initialized as null.
06:03 FileReader object can be used to access and read a particular file.
06:08 Eclipse will show an error.
06:11 To rectify the error, click on it and double click import FileReader java dot io.
06:19 The FileReader class is imported from the java dot io package.
06:25 We will learn about package and its usage in detail in a later tutorial.
06:31 To allow fr to access a file called Marks which is located in the home folder, type the following code.
06:40 The path shown here is to be replaced with that of your system's home folder.
06:46 Now an error comes up. It indicates that this line of code can create a FileNotFoundException.
06:55 Click on the error and double click Surround with try/catch.
07:00 We can see that Eclipse automatically inserts the try catch block to rectify this error.
07:08 So we can understand that this is a checked exception.
07:12 Next let us see how to use finally block.
07:16 Type the following code.
07:18 finally block usually follows a try-catch block .
07:22 The code inside this block is executed whether exception has occurred or not. It contains a print statement.
07:32 Now let us close the file reference inside the finally block.
07:37 So type, fr dot close
07:40 Now Eclipse indicates that this will raise an IOException.
07:45 So click on the error and double click Surround with try/catch.
07:51 Now let us run the program.
07:54 We can see that FileNotFoundException message is printed.
07:59 This is because we don't have a file named Marks in our home folder.
08:04 We can also see a NullPointerException as fr still refers to a null value.
08:12 But we can see that the print statement inside finally block gets executed.
08:18 Let us now create a text file Marks in our home folder.
08:23 If you are a windows user, create a text file in your local drive and mention its path.
08:29 For example it can be specified as D:\\Marks.txt
08:37 Let us now run the program again.
08:40 We can verify that there are no exceptions once the Marks file is created.
08:46 And “Inside finally block” gets printed.
08:50 The cleanup operation i.e closing the FileReader Object fr is also executed successfully.
08:58 With this we come to the end of this tutorial.
09:02 Let us summarize.
09:04 In this tutorial we have learnt about: What is an Exception Checked and Unchecked Exceptions Handling the Exceptions using the try-catch block finally block .
09:17 As an assignment, Learn about another Runtime Exception called NullPointerException.
09:24 Refer to the Java program named Demo.java provided in the Assignment link of this tutorial.
09:31 An exception will be raised when you run this code.
09:35 Identify the code which is responsible for the exception.
09:40 Rectify it using a try-catch block.
09:43 The video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
09:52 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials , Gives certificates on passing the online tests.For more details, please write to us.
10:04 Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India. More information on this Mission is available at the link shown.
10:15 This script has been contributed by:Dept. of Information Technology, Amal Jyothi College of Engineering.
10:23 This is Priya from IIT Bombay. Thanks for joining.

Contributors and Content Editors

Jyotisolanki