Exception Handling in Java
Exception Handling in Java
Mohana
Assistant Professor
SNR
Exception Handling in Java
Error
• An error may produce an incorrect output
or may terminate the execution of the
program abruptly or even may cause the
system to crash.
• It is our responsibility to detect and
manage the error properly.
Types of error
• Runtime Errors: occur while the program
is running if the environment detects an
operation that is impossible to carry out.
• Logic Errors: occur when a program
doesn't perform the way it was intended
• Syntax Errors: Arise because the rules of
the language have not been followed.
They are detected by the compiler.
Example of Run Time error
Class Error
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x=a/(b+c);
System.out.println("x=" +x);
int y=a/(b-c); // Errorr division by zero
System.out.println("y=" +y);
}
}
Fundamentals of Exception
Handling
• An Exception is a condition that is caused
by a runtime error in the program.
• A Java Exception is an object that
describes an exceptional condition that
has occurred in a piece of code .
• The purpose of the exception handling
mechanism is to provide a means to
detect and report an “exceptional
circumstances” .
…contd
• In Java, try, catch, throw, and finally are
keywords used for exception handling,
which is a mechanism to deal with runtime
errors and maintain the normal flow of a
program.
• Types:NullPointerException, ArithmeticExc
eption, ArrayIndexOutOfBoundsException,
and NumberFormatException.
Exceptional Handling
try Block:
• The try block encloses a segment of code that
is suspected of potentially throwing an
exception.
Catch block:
• The catch block immediately follows
a try block and is used to handle specific types
of exceptions that might be thrown within
the try block.
..contd
throw Keyword:
• The throw keyword is used to explicitly throw
an exception from within a method or a block
of code.
• Example:
public void validateAge(int age)
{
if (age < 18)
{
throw new IllegalArgumentException("Age must be 18
or older.");
}
// Further processing if age is valid
}
….contd
• finally block is an optional block that
follows try and catch blocks.
• The code within the finally block is
guaranteed to execute regardless of
whether an exception was thrown or not,
or whether it was caught.
SYNTAX
try
{
… normal program code
}
catch(Exception e)
{
… exception handling code
}
Example
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Error: Array index out of bounds.");
}
finally {
System.out.println("Cleanup operations performed.");
}
Example
class error2
{
public static void main(String arg[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println(“Division by Zero”);
}
Y=a/(b-c);
System.out.println(“y=“+y);
}
}
Mulitple Catch Block
• In Java, a try block can be followed by
multiple catch blocks to handle different types
of exceptions that might occur within
the try block.
• This allows for specific handling logic based on
the type of exception encountered.
try
Example
{
}
catch (ExceptionType1 e1)
{
}
catch (ExceptionType2 e2)
{
}
catch (Exception e)
{ // General Exception catch (optional)
// Handle any other Exception
}
Conclusion
• Exceptions are a powerful error handling
mechanism.
• Exceptions in Java are built into the language.
• Exceptions can be handled by the programmer
(try-catch), or handled by the Java environment
(throws).
• Exception handling can only hide the errors.
• It cannot correct the errors.