Chapter 10
Exception Handling in Java
Types of errors in Java
Compile-time error
Run-time error
Logical error
Types of errors in Java
Compile Time Errors.
These errors are syntactical errors, found at the time of compilation by the compiler. Ex: missing
semicolon, missing flower bracket, missing quotes etc.
Run Time Errors.
Sometimes JVM would be enabling to execute statements due to some reasons. These errors are
called as run-time errors.
❖ Divide by zero (1/0).
❖ Accessing an element out of bounds of an array.
❖ Reading data from an already closed file.
Types of errors in Java
Logical Errors
Sometimes programs may produce wrong results due to wrong logic or may terminate due to some
reasons.
Exception and Exception handling
Exception: “An error occurs at runtime is called an Exception.”
Exception Handling
“The mechanism of handling runtime errors or exceptions is called Exception handling.”
The process of handling the errors in JVM in an organized manner is known as Exception Handling.
If an exception is not handled in the program, the program stops abruptly.
If an exception is handled in the program, the program will resume execution after the exception is
handled.
Types of Exceptions
Exception and Exception handling
Exception: “An error occurs at runtime is called an Exception.”
Exception Handling
“The mechanism of handling runtime errors or exceptions is called Exception handling.”
The process of handling the errors in JVM in an organized manner is known as Exception Handling.
If an exception is not handled in the program, the program stops abruptly.
If an exception is handled in the program, the program will resume execution after the exception is
handled.
Checked Exception
Checked exceptions are called compile-time Exceptions.
These exceptions are checked at compile-time by the compiler.
The compiler ensures whether the programmer handles the exception or not. The programmer
should have to handle the exception; otherwise, the system has shown a compilation error.
Eg: IOException, FileNotFoundException, InterruptedException etc..
Unchecked Exception
They are also called as Run Time Exceptions.
The java compiler doesn’t enforce programmatic handling of RunTimeExceptions.
Hence they are referred as unchecked exceptions.
Usually, it occurs when the user provides bad data during the interaction with the program.
Eg:
ArithmeticException,
ArrayIndexOutOfBoundsException
Difference between error and exception.
Exception is an error which can be handled.
An error is an error which cannot be handled as it is generated by the JVM itself.
Exception Handling
An exception is an event that disrupts the normal flow of the program. It is an object which is
thrown at runtime.
Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException,
IOException etc..
The core advantage of exception handling is to maintain the normal flow of the application.
Exception Handling
The core of Exception Handling is try and catch.
These Keywords works together.
There will be no try without a catch or a catch without a try.
Java provides the exception handling constructs like try-catch, try – catch – finally to manage
runtime errors.
Exception handling includes the following steps
Hit the Exception .
Throw the Exception.
Catch the Exception
Handle the Exception
Approaches to handle exceptions in Java.
1. try...catch block
2. finally block
3. throw and throws keyword
try catch block
The try-catch block is used to handle exceptions in Java.
syntax of try...catch block:
try{
//code
}
catch(Exception e){
//code
}
Here, we have placed the code that might generate an exception inside the try block. Every try block
is followed by a catch block. When an exception occurs, it is caught by the catch block.
The catch block cannot be used without the try block.
final block
“A finally block of code always executes whether an Exception as occurred or not.”
Finally block appears at the end of the catch block or at the end of try block.
The finally block is optional. And, for each try block, there can be only one finally block.
final block
The basic syntax of finally block is:
If an exception occurs, the finally block is executed after
the try...catch block.
Otherwise, it is executed after the try block. For
each try block, there can be only one finally block.
We generally use the finally block to execute clean up code
like closing connections, closing files, or freeing up threads,
as it executes regardless of an exception
throw and throws keyword
The Java throw keyword is used to explicitly throw a single exception and handle it using catch
block.
When we throw an exception, the flow of the program moves from the try block to the catch block.
Syntax:
throw new ThrowableExceptionObject;
throw and throws keyword
The throws keyword is used when the programmer doesn’t wants to handle the exception in the
method(inside the method) .
The method must declare it using the ‘throws’ keyword.
‘throws’ keyword appears at the end of a method signature
End of Chapter