Java/C3/Custom-Exceptions/English
Title of script: Custom Exceptions
Author: Joms Antony
Keywords: Custom Exceptions, User defined exceptions, throw keyword, throws keyword, video tutorial, Java
|
|
Slide 1
|
Welcome to the spoken tutorial on custom exceptions. |
Slide 2
Learning Objectives
|
In this tutorial we will learn about:
|
Slide 3
Software Requirements
|
To record this tutorial, I am using
|
Slide 4
Prerequisites
|
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. |
Slide 5
Custom Exception
|
First let us learn about custom exceptions.
|
In Eclipse IDE, create a project called CustomExceptionDemo | Now we will open eclipse and create a new project called CustomExceptionDemo.
|
Right click on src folder and click New-> Class and type the class name as InvalidMarkException and hit Enter | We will create a new class InvalidMarkException.
|
public class InvalidMarkException
Type extends Exception |
To make this as a type of exception class, it should be a subclass of Java exception class.
|
Click on Source -> Generate constructors from Superclass
|
Click on Source menu and then select Generate constructors from Superclass.
|
Click on Deselect All
|
Now click on Deselect All button in the right hand side. |
Select Exception(String)
Click OK button.
|
Then select the constructor with a single string argument and click on OK button at the bottom.
|
Right click on src folder and click New-> Class -> StudentMarks | Let us add another class named StudentMarks. |
Copy the following code
int marks; public StudentMarks(int marks) { this.marks = marks; } Highlight “int marks;” Highlight “public StudentMarks(int marks) { this.marks = marks;}” |
Then type the following code.
This constructor initializes the value of marks. |
Copy the following code
public void validate() { if (marks<0 || marks>100)
else System.out.println("Entry OK"); }
Highlight “throw new InvalidMarkException(marks+" not a valid entry");”
|
Let us now add a method to validate the marks.
|
Highlight
throw new InvalidMarkException(marks+" is not a valid entry"); |
We can see that there is an error InvalidMarkException.
|
Click on the error and double click “Add throws declaration”
|
So click on the error and double click “Add throws declaration”.
|
Highlight
public void validate() throws InvalidMarkException |
Here we can see that the throws keyword is used along with methods. |
Highlight
InvalidMarkException |
It indicates that the method will raise the specified exception. |
throw new InvalidMarkException(marks+" not a valid entry"); | We have to provide the exception handling code when such a method is called.
|
Next to System.out.println("Entry OK"); statement, type
FileReader fr=null; |
Next, let us perform a file access operation which will raise a FileNotFoundException.
|
Point the error
|
Eclipse will show some errors as we have not imported the corresponding Java packages.
|
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.
|
Point the error
Click on the error and double click Add throws declaration
|
An error shows that this line of code can raise a FileNotFoundException.
|
Highlight FileNotFoundException
|
We can see that FileNotFoundException is also added to the throws clause.
|
Copy the following code
public static void main(String[] args) { StudentMarks m1=new StudentMarks(40);
} |
We will now create the main method inside the StudentMarks class and verify the results.
|
Highlight StudentMarks m1=new StudentMarks(40);
|
Here we create an object m1 initialized with 40 as the value for marks.
In the next line we invoke the method validate using the object m1. |
Point the error | We can see that there is an error when the validate method is invoked.
InvalidMarkException and FileNotFoundException
|
Point to throws in the drop down options
|
To resolve the error, we can add throws clause to main method as we did earlier
|
Click run icon | Now let us run this program. |
Point to Entry OK, rest of the code | It displays “Entry OK” and “rest of the code”.
|
Modify “Marks m1=new Marks(-10);” | Let us now change the value to -10 which is an invalid entry. |
Click run icon | We will run the program again. |
Highlight the output
InvalidMarkException: -10 is not a valid entry
|
Now we can see that the InvalidMarkException is thrown as -10 is an invalid entry.
we can see the message “rest of the code”
this message “rest of the code” will not be printed.
|
Slide 8
Summary
|
Let us summarize.
|
Slide 9
Assignment
InvalidAgeException
|
As an assignment
|
Slide 9A
Assignment
try-catch blocks
|
Create objects inside the main
method and invoke the validate() method
try-catch blocks wherever required.
|
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 and • 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:
Department of Information Technology, Amal Jyothi College of Engineering
|