Java/C3/Custom-Exceptions/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on custom exceptions.
00:05 In this tutorial we will learn about: Custom exceptions and Usage of throw and throws keywords
00:14 To record this tutorial, I am using :Ubuntu Linux 16.04 OS JDK 1 .7 and Eclipse 4.3.1
00:26 To follow this tutorial, you must have basic knowledge of Exceptions Handling in Java. If not, for relevant Java tutorials, please visit the link shown.
00:38 First let us learn about custom exceptions.
00:42 Custom exception is a user defined exception class. It is usually created as checked exceptions.
00:51 It is used to customize the exception according to user need.
00:57 Now we will open eclipse and create a new project called CustomExceptionDemo.
01:04 Inside this project we will create the necessary classes to demonstrate custom exceptions.
01:11 We will create a new class InvalidMarkException.
01:15 To make this as a type of exception class, it should be a subclass of Java exception class.
01:22 To do so, type extends Exception.
01:27 Click on Source menu and then select Generate constructors from Superclass.
01:34 Now click on Deselect All button in the right hand side.
01:38 Then select the constructor with a single string argument and click on OK button at the bottom.
01:45 This string argument can be used to customize the message shown when this exception occurs.<<PAUSE>>
01:52 Let us add another class named StudentMarks.
01:57 Then type the following code.
02:00 This class contains only one variable named marks.
02:04 This constructor initializes the value of marks.
02:09 Let us now add a method to validate the marks.
02:13 The normal range of marks is from 0 to 100.
02:18 If marks less than 0 or greater than 100 is processed, InvalidMarkException will be thrown.
02:25 For this, we need to use the throw keyword explicitly to throw a custom exception.
02:33 If the mark is a valid one, the message “Entry OK” will be displayed.
02:39 We can see that there is an error InvalidMarkException.
02:43 Let us check and resolve it.
02:46 So click on the error and double click “Add throws declaration”.
02:51 We can see that the error disappears once the “throws InvalidMarkException” is added to the method signature.
03:00 Here we can see that the throws keyword is used along with methods.
03:06 It indicates that the method will raise the specified exception.
03:11 We have to provide the exception handling code when such a method is called.
03:16 Next, let us perform a file access operation which will raise a FileNotFoundException.
03:23 So type the following code to create an instance of a FileReader class.
03:29 Eclipse will show some errors as we have not imported the corresponding Java packages.
03:36 To rectify the same, click on the error and then double click import 'FileReader' (java.io).
03:44 We will learn about package and its usage in detail in a later tutorial.
03:50 To allow fr to access a file called Marks which is located in the home folder, type the following code.
03:59 The path shown here is to be replaced with that of your system's home folder.
04:05 An error shows that this line of code can raise a FileNotFoundException.
04:10 We can resolve it by adding this exception in the throws clause.
04:16 We can see that FileNotFoundException is also added to the throws clause.
04:22 We can handle multiple exceptions using throws as shown here.
04:28 We will now create the main method inside the StudentMarks class and verify the results.
04:34 Here we create an object m1 initialized with 40 as the value for marks.
04:41 In04: the next line we invoke the method validate using the object m1.
04:47 We can see that there is an error when the validate method is invoked.
04:52 It says that this method will raise the InvalidMarkException and FileNotFoundException
04:59 To resolve the error, we can add throws clause to main method as we did earlier
05:05 But it is recommended to use try and catch block.
05:10 So, double click Surround with try/catch.
05:14 Now the necessary try-catch blocks are added and the exception has been handled.
05:20 Now let us run this program.
05:23 It displays “Entry OK” and “rest of the code”.
05:27 This happens because the value of marks 40 is a valid entry.
05:32 Let us now change the value to -10 which is an invalid entry.
05:37 We will run the program again.
05:40 Now we can see that the InvalidMarkException is thrown as -10 is an invalid entry.
05:47 Since we have handled the exception, we can see the message “rest of the code”
05:53 Instead if we use “throws” clause, this message “rest of the code” will not be printed.
06:00 Also the program will be terminated.
06:03 So it is better to use a try catch block when a method is called inside the main method.
06:10 With this we come to the end of this tutorial.
06:13 Let us summarize.
06:15 In this tutorial we have learned about :What is a Custom Exception Usage of throw and throws keywords How to create and use custom exceptions.
06:26 As an assignment :Create a custom exception class called InvalidAgeException
06:33 Create another class Age and create a constructor to initialize the value of age
06:39 Also create a method validate to throw an exception if the age is less than 18
06:45 Create objects inside the main method and invoke the validate() method
06:51 Provide exception handling using try-catch blocks wherever required.
06:56 Verify the custom exception class.
07:00 The video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
07:06 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials and Gives certificates on passing the online tests For more details, please write to us.
07:18 Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India. More information on this mission is available at the link shown.
07:29 This script has been contributed by: Department of Information Technology, Amal Jyothi College of Engineering


07:36 This is Priya from IIT Bombay, signing off. Thanks for joining.

Contributors and Content Editors

Jyotisolanki