0% found this document useful (0 votes)
5 views11 pages

Java Programming Lab 7

The document discusses exception handling in Java, explaining its importance in managing runtime errors without halting program execution. It outlines the types of built-in exceptions, including checked, unchecked, and errors, as well as the creation of user-defined exceptions. Additionally, it covers the use of try-catch blocks, the finally block, and the throw keyword for explicitly throwing exceptions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Java Programming Lab 7

The document discusses exception handling in Java, explaining its importance in managing runtime errors without halting program execution. It outlines the types of built-in exceptions, including checked, unchecked, and errors, as well as the creation of user-defined exceptions. Additionally, it covers the use of try-catch blocks, the finally block, and the throw keyword for explicitly throwing exceptions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming Lab

Session 7
By : Dr. Amar Arora
USAR, GGSIPU-EDC
Exception Handling

• Exception handling java iis an useful mechanism to tackle


runtime errors like divide by zero, class not found, etc.
• By using the mechanism the program will not halt and
continue its remaining operation after handling of the run
time error.
Exception Hierarchy (GeekForGeeks)
Types of Built In Exception [[Link]]

1. Checked Exception: These are the exceptions that are


checked by the compiler at compile time. If a method
throws a checked exception, then the caller of the method
must either handle the exception or declare it in the throws
clause. E.g. SQLException, ClassNotFoundException
2. Unchecked Exception: These are the exceptions that are
not checked by the compiler at compile time. E.g.
NullPointerException, ArithmeticException, etc.
3. Error: The Error class represents the serious problems
that cause the program to abort. E.g. out of memory error,
stack overflow error
User Defined Exceptions
1. Java provides mechanism to create our own exception class by extending
the Exception class.
2. Then these exceptions can be thrown using the throw keyword.
E.g. class UserDefinedException extends Exception
{ public UserDefinedException(String s)
{
// Call constructor of parent Exception
super(s);
}
public String toString(){
return ("User Defined Exception”) ; }
}
Handling Exceptions

try
{
Int divideByZero=9/0;
}catch (ArithmeticException ex)
{
[Link](“Divide by zero not allowed”);
}
Try with multiple catch blocks

try
{
Int divideByZero=9/0;
}catch (ArithmeticException ex)
{[Link](“Divide by zero not allowed”);}
catch (Exception ex){[Link](“Divide by zero not
allowed”);}
Use of Finally block

Finally block will be at the end of try catch and it is meant to execute
in all the three cases:
1. Exception occurred and caught
2. Exception occurred but not handled
3. Exception did not occur
try
{
}catch(Exception ex){}
finally{}
throw keyword

throw keyword in java is used to throw an exception explicitly.


We specify the exception object which is to be thrown. The
Exception has some message with it that provides the error
description.

E.g. throw new ArithmaticException(“Less than zero not


allowed”);
throw new UserDefinedException(“UserDefinedException”);
Example of Custom Exception

A very logical example to handle age of voter has been


demonstrated by Java point at
[Link]
Program to Execute
13. Make a class to catch an exception created by
division by zero along with finally block.
14. Make an user defined exception
InalidMobileNumberException and thrown when the
mobile number entered by user is invalid format.

You might also like