Difference between revisions of "Java/C3/Custom-Exceptions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script:''' Custom Exceptions '''Author:''' Joms Antony '''Keywords:''' Custom Exceptions, User defined exceptions, throw keyword, throws keyword, video tutorial,...")
 
 
Line 12: Line 12:
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 1'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 1'''
 
  
  
Line 23: Line 22:
  
 
* '''Custom exceptions'''
 
* '''Custom exceptions'''
* Usage of '''throw''' and '''throws''' keywords
+
* Usage of '''throw''' and '''throws keywords'''
 
+
 
+
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;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.173cm;padding-right:0.191cm;"| In this tutorial we will learn about:
  
 
* '''Custom exceptions '''and
 
* '''Custom exceptions '''and
* Usage of '''throw''' and '''throws''' keywords
+
* Usage of '''throw''' and '''throws keywords'''
 
+
 
+
  
 
|-
 
|-
Line 41: Line 36:
 
* '''JDK 1 .7'''
 
* '''JDK 1 .7'''
 
* '''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.173cm;padding-right:0.191cm;"| To record this tutorial, I am using
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To record this tutorial, I am using
  
Line 48: Line 41:
 
* '''JDK 1 .7 '''and
 
* '''JDK 1 .7 '''and
 
* '''Eclipse 4.3.1 '''
 
* '''Eclipse 4.3.1 '''
 
 
  
 
|-
 
|-
Line 58: Line 49:
 
* Basic knowledge of '''Exceptions Handling'''
 
* Basic knowledge of '''Exceptions Handling'''
 
* 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.173cm;padding-right:0.191cm;"| To follow this tutorial, you must have basic knowledge of '''Exceptions Handling '''in''' Java.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To follow this tutorial, you must have basic knowledge of '''Exceptions Handling '''in''' Java.'''
  
Line 70: Line 59:
  
 
* '''Custom exception''' is a user defined exception class.
 
* '''Custom exception''' is a user defined exception class.
* It is usually created as checked exceptions.
+
* It is usually created as '''checked exceptions'''.
* It is used to customize the exception according to user need.
+
* It is used to customize the '''exception''' according to user need.
  
  
Line 81: Line 70:
 
* It is usually created as '''checked exceptions.'''
 
* It is usually created as '''checked exceptions.'''
  
* It is used to customize the exception according to user need.  
+
* It is used to customize the '''exception''' according to user need.  
 
+
 
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| In Eclipse IDE, create a project called '''CustomExceptionDemo '''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| In Eclipse IDE, create a project called '''CustomExceptionDemo '''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will open''' eclipse''' and create a new project called''' CustomExceptionDemo.'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will open''' eclipse''' and create a new '''project''' called''' CustomExceptionDemo.'''
 
+
 
+
Inside this project we will create the necessary classes to demonstrate '''custom exceptions.'''
+
 
+
  
  
 +
Inside this '''project''' we will create the necessary '''classes''' to demonstrate '''custom exceptions.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Right click on '''src''' folder and click '''New'''-> '''Class '''and type the class name as '''InvalidMarkException''' and hit '''Enter'''  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Right click on '''src''' folder and click '''New'''-> '''Class '''and type the class name as '''InvalidMarkException''' and hit '''Enter'''  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We will create a new '''class InvalidMarkException.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We will create a new '''class InvalidMarkException.'''
 
 
 
  
 
|-
 
|-
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''public''' '''class''' InvalidMarkException
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''public class InvalidMarkException '''
  
 
Type
 
Type
  
'''extends''' '''Exception'''  
+
'''extends Exception'''  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To make this as a type of '''exception''' class, it should be a subclass of Java '''exception''' class.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To make this as a type of '''exception class''', it should be a '''subclass''' of '''Java exception class'''.
  
  
To do so, type''' extends''' '''Exception'''.
+
To do so, type''' extends Exception'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''Source''' -> '''Generate constructors from Superclass'''  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''Source''' -> '''Generate constructors from Superclass'''  
 
 
  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''Source''' menu and then select '''Generate constructors from Superclass'''.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''Source''' menu and then select '''Generate constructors from Superclass'''.
 
 
 
  
 
|-
 
|-
Line 137: Line 113:
  
 
Highlight the inserted code
 
Highlight the inserted code
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Then select the constructor with a single string argument and click on '''OK''' button at the bottom.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Then select the '''constructor''' with a '''single string argument''' and click on '''OK''' button at the bottom.
  
  
This '''string''' argument can be used to customize the message shown when this '''exception''' occurs.
+
This '''string argument''' can be used to customize the message shown when this '''exception''' occurs.
  
  
Line 147: Line 123:
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Right click on '''src''' folder and click '''New'''-> '''Class -> StudentMarks'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Right click on '''src''' folder and click '''New'''-> '''Class -> StudentMarks'''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us add another class named '''StudentMarks.'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us add another '''class''' named '''StudentMarks.'''
  
 
|-
 
|-
Line 166: Line 142:
  
  
This class contains only one variable named '''marks.'''
+
This '''class''' contains only one '''variable''' named '''marks.'''
  
This constructor initializes the value of '''marks.'''
+
This '''constructor''' initializes the value of '''marks.'''
  
 
|-
 
|-
Line 198: Line 174:
  
 
Highlight "'''Entry OK'''"
 
Highlight "'''Entry OK'''"
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us now add a method to validate the marks.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us now add a '''method''' to validate the marks.
  
  
Line 207: Line 183:
  
  
For this, we need to use the '''throw''' keyword explicitly to throw a custom exception.
+
For this, we need to use the '''throw keyword''' explicitly to throw a '''custom exception'''.
  
  
Line 225: Line 201:
  
  
Highlight the “'''throws''' InvalidMarkException”
+
Highlight the “'''throws InvalidMarkException”'''  
  
  
Line 232: Line 208:
  
  
We can see that the error disappears once the '''“throws InvalidMarkException”''' is added to the method signature.
+
We can see that the error disappears once the '''“throws InvalidMarkException”''' is added to the '''method signature.'''
 
+
 
+
 
+
  
 
|-
 
|-
Line 241: Line 214:
  
 
'''public''' '''void''' validate() '''throws''' InvalidMarkException
 
'''public''' '''void''' validate() '''throws''' InvalidMarkException
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Here we can see that the '''throws''' keyword is used along with methods.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Here we can see that the '''throws keyword''' is used along with '''methods'''.
  
 
|-
 
|-
Line 247: Line 220:
  
 
InvalidMarkException
 
InvalidMarkException
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| It indicates that the method will raise the specified exception.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| It indicates that the '''method''' will raise the '''specified exception.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''throw''' '''new''' InvalidMarkException(marks+" not a valid entry");
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''throw''' '''new''' InvalidMarkException(marks+" not a valid entry");
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We have to provide the exception handling code when such a method is called.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We have to provide the '''exception handling''' code when such a '''method''' is called.
  
  
Line 260: Line 233:
  
 
'''FileReader fr=null;'''
 
'''FileReader fr=null;'''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Next, let us perform a file access operation which will raise a '''FileNotFoundException'''.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Next, let us perform a '''file access operation''' which will raise a '''FileNotFoundException'''.
  
  
So type the following code to create an instance of a '''FileReader '''class'''.'''
+
So type the following code to create an instance of a '''FileReader class'''.
  
 
|-
 
|-
Line 314: Line 287:
  
 
Highlight '''throws''' InvalidMarkException, FileNotFoundException
 
Highlight '''throws''' InvalidMarkException, FileNotFoundException
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We can see that '''FileNotFoundException''' is also added to the '''throws''' clause.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We can see that '''FileNotFoundException''' is also added to the '''throws clause'''.
  
  
We can handle multiple exceptions using '''throws''' as shown here.
+
We can handle multiple '''exceptions''' using '''throws''' as shown here.
  
  
Line 336: Line 309:
  
 
}
 
}
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We will now create the '''main''' method inside the '''StudentMarks '''class and verify the results.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We will now create the '''main method''' inside the '''StudentMarks class''' and verify the results.
  
  
Line 352: Line 325:
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Point the error
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Point the error
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We can see that there is an error when the '''validate''' method is invoked.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We can see that there is an error when the '''validate method''' is invoked.
  
  
It says that this method will raise the  
+
It says that this '''method''' will raise the  
  
 
'''InvalidMarkException''' and '''FileNotFoundException'''
 
'''InvalidMarkException''' and '''FileNotFoundException'''
 
 
 
  
 
|-
 
|-
Line 370: Line 340:
  
  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To resolve the error, we can add '''throws''' clause to '''main''' method as we did earlier
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To resolve the error, we can add '''throws clause''' to '''main method''' as we did earlier
  
  
But it is recommended to use '''try''' and '''catch''' '''block.'''
+
But it is recommended to use '''try''' and '''catch block.'''
  
  
Line 379: Line 349:
  
  
Now the necessary '''try-catch blocks''' are added and the exception has been handled.
+
Now the necessary '''try-catch blocks''' are added and the '''exception''' has been handled.
  
 
|-
 
|-
Line 410: Line 380:
  
  
Since we have handled the exception,
+
Since we have handled the '''exception''', we can see the message “rest of the code”
  
we can see the message “rest of the code”
 
  
 
+
Instead if we use '''“throws” clause''', this message “rest of the code” will not be printed.  
Instead if we use “throws” clause,
+
 
+
this message “rest of the code” will not be printed.  
+
  
  
Line 423: Line 389:
  
  
So it is better to use a '''try catch block''' when a method is called inside the '''main''' method.
+
So it is better to use a '''try catch block''' when a '''method''' is called inside the '''main method'''.
  
  
Line 437: Line 403:
  
  
* What is a Custom Exception
+
* What is a '''Custom Exception'''
* Usage of throw and throws keywords  
+
* Usage of '''throw''' and '''throws keywords'''
* How to create and use custom exceptions  
+
* How to create and use '''custom exceptions'''
 
+
 
+
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us summarize.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us summarize.
  
Line 447: Line 411:
 
In this tutorial we have learned about
 
In this tutorial we have learned about
  
* What is a Custom Exception  
+
* What is a '''Custom Exception'''
* Usage of throw and throws keywords  
+
* Usage of '''throw''' and '''throws keywords'''
* How to create and use custom exceptions
+
* How to create and use '''custom exceptions'''
 
+
 
+
  
 
|-
 
|-
Line 459: Line 421:
  
  
* Create a custom exception class  
+
* Create a '''custom exception class InvalidAgeException'''
  
'''InvalidAgeException'''
+
* Create another '''class Age''' and create a '''constructor''' to initialize the value of age  
 
+
* Create '''validate() method''' to throw the above exception if age < 18  
* Create another class '''Age''' and create a constructor to initialize the value of age  
+
* Create validate() method to<nowiki> throw the above exception if age < 18 </nowiki>
+
  
  
Line 470: Line 430:
  
  
Create a custom exception class called '''InvalidAgeException'''  
+
Create a '''custom exception class''' called '''InvalidAgeException'''  
  
  
Create another class '''Age''' and create a constructor to initialize the value of age
+
Create another '''class Age''' and create a '''constructor''' to initialize the value of age
  
  
Also create a method '''validate''' to throw an exception if the age is less than 18
+
Also create a '''method validate''' to throw an '''exception''' if the age is less than 18
  
 
|-
 
|-
Line 484: Line 444:
  
  
* Create objects inside the main method and invoke the validate() method
+
* Create objects inside the '''main method''' and invoke the '''validate() method'''
 
+
* Provide exception handling using
+
 
+
try-catch blocks
+
 
+
* Verify the custom exception class
+
 
+
 
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Create objects inside the '''main'''  
+
 
+
method and invoke the '''validate()''' method
+
 
+
  
Provide exception handling using  
+
* Provide '''exception handling''' using '''try-catch blocks'''
  
'''try-catch''' blocks wherever required.
+
* Verify the '''custom exception class '''
  
 +
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Create objects inside the '''main method''' and invoke the '''validate() method'''
  
Verify the custom exception class.
 
  
 +
Provide '''exception handling''' using '''try-catch blocks''' wherever required.
  
  
 +
Verify the '''custom exception class'''.
  
 
|-
 
|-

Latest revision as of 15:58, 16 June 2017

Title of script: Custom Exceptions

Author: Joms Antony

Keywords: Custom Exceptions, User defined exceptions, throw keyword, throws keyword, video tutorial, Java


Visual Cue
Narration
Slide 1


Welcome to the spoken tutorial on custom exceptions.
Slide 2

Learning Objectives

  • Custom exceptions
  • Usage of throw and throws keywords
In this tutorial we will learn about:
  • Custom exceptions and
  • Usage of throw and throws keywords
Slide 3

Software Requirements

  • Ubuntu Linux 16.04
  • JDK 1 .7
  • Eclipse 4.3.1
To record this tutorial, I am using
  • Ubuntu Linux 16.04 OS
  • JDK 1 .7 and
  • Eclipse 4.3.1
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

  • Custom exception is a user defined exception class.
  • It is usually created as checked exceptions.
  • It is used to customize the exception according to user need.


First let us learn about custom exceptions.


  • Custom exception is a user defined exception class.
  • It is usually created as checked exceptions.
  • It is used to customize the exception according to user need.
In Eclipse IDE, create a project called CustomExceptionDemo Now we will open eclipse and create a new project called CustomExceptionDemo.


Inside this project we will create the necessary classes to demonstrate custom exceptions.

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.


To do so, type extends Exception.

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.


Highlight the inserted code

Then select the constructor with a single string argument and click on OK button at the bottom.


This string argument can be used to customize the message shown when this exception occurs.


<<PAUSE>>

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 class contains only one variable named marks.

This constructor initializes the value of marks.

Copy the following code

public void validate()

{

if (marks<0 || marks>100)


throw new InvalidMarkException(marks+" is not a valid entry");

else

System.out.println("Entry OK");

}


Highlight “if (marks<0 || marks>100)”

Highlight “throw new InvalidMarkException(marks+" not a valid entry");”


Highlight “throw” and “marks not a valid entry”


Highlight "Entry OK"

Let us now add a method to validate the marks.


The normal range of marks is from 0 to 100.


If marks less than 0 or greater than 100 is processed, InvalidMarkException will be thrown.


For this, we need to use the throw keyword explicitly to throw a custom exception.


If the mark is a valid one, the message “Entry OK” will be displayed.

Highlight

throw new InvalidMarkException(marks+" is not a valid entry");

We can see that there is an error InvalidMarkException.


Let us check and resolve it.

Click on the error and double click “Add throws declaration”


Highlight the “throws InvalidMarkException”


So click on the error and double click “Add throws declaration”.


We can see that the error disappears once the “throws InvalidMarkException” is added to the method signature.

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.


<<PAUSE>>

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.


So type the following code to create an instance of a FileReader class.

Point the error


Click on the error and double click import 'FileReader' (java.io).


Eclipse will show some errors as we have not imported the corresponding Java packages.


To rectify the same, click on the error and then double click import 'FileReader' (java.io).


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

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


Highlight "/home/spoken/Marks


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.

Point the error


d

Click on the error and double click Add throws declaration


An error shows that this line of code can raise a FileNotFoundException.


We can resolve it by adding this exception in the throws clause.

Highlight FileNotFoundException


Highlight throws InvalidMarkException, FileNotFoundException

We can see that FileNotFoundException is also added to the throws clause.


We can handle multiple exceptions using throws as shown here.


<<PAUSE>>

Copy the following code

public static void main(String[] args) {

StudentMarks m1=new StudentMarks(40);


m1.validate();


System.out.println("rest of the code");

}

We will now create the main method inside the StudentMarks class and verify the results.



Highlight StudentMarks m1=new StudentMarks(40);


Highlight m1.validate();

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.


It says that this method will raise the

InvalidMarkException and FileNotFoundException

Point to throws in the drop down options


Double click Surround with try/catch


To resolve the error, we can add throws clause to main method as we did earlier


But it is recommended to use try and catch block.


So, double click Surround with try/catch.


Now the necessary try-catch blocks are added and the exception has been handled.

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”.


This happens because the value of marks 40 is a valid entry.

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


Highlight the message “rest of the code”

Now we can see that the InvalidMarkException is thrown as -10 is an invalid entry.


Since we have handled the exception, we can see the message “rest of the code”


Instead if we use “throws” clause, this message “rest of the code” will not be printed.


Also the program will be terminated.


So it is better to use a try catch block when a method is called inside the main method.


With this we come to the end of this tutorial.


<<PAUSE>>

Slide 8

Summary


  • What is a Custom Exception
  • Usage of throw and throws keywords
  • How to create and use custom exceptions
Let us summarize.


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
Slide 9

Assignment


  • Create a custom exception class InvalidAgeException
  • Create another class Age and create a constructor to initialize the value of age
  • Create validate() method to throw the above exception if age < 18


As an assignment


Create a custom exception class called InvalidAgeException


Create another class Age and create a constructor to initialize the value of age


Also create a method validate to throw an exception if the age is less than 18

Slide 9A

Assignment


  • Create objects inside the main method and invoke the validate() method
  • Provide exception handling using try-catch blocks
  • Verify the custom exception class
Create objects inside the main method and invoke the validate() method


Provide exception handling using try-catch blocks wherever required.


Verify the custom exception class.

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 and

• 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:

Department of Information Technology, Amal Jyothi College of Engineering


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

Contributors and Content Editors

Nancyvarkey, Priyacst