Exception Handling in Java
Exception Handling in Java
class ExceptionExample {
public static void main(String args[]) {
System.out.println("Welcome to ScholarHat");
int a = 30;
int b = 0;
System.out.println(a / b);
System.out.println("Welcome to the ScholarHat's Java Programming tutorial.");
System.out.println("Enjoy your learning");
}
}
Try it Yourself >>
In the above code, at the 4th line, an integer is divided by 0, which is not
possible, and the JVM(Java Virtual Machine) raises an exception. In this
case, the programmer does not handle the exception, which will halt the
program in between by throwing the exception, and the rest of the lines of
code won't be executed.
Output
Welcome to ScholarHat
1. Built-in Exceptions
These exceptions are present in Java libraries.
There are two types of built-in exceptions in Java:
1. Checked Exception
Checked exceptions are classes that inherit directly from the Throwable class,
with the exception of RuntimeException and Error. Examples include
IOException, SQLException, and so on. Checked exceptions are checked at
compilation time. They must be either caught by the code or declared in the
method signature using the throws keyword.
2. Unchecked Exception
Classes that inherit the RuntimeException class are known as unchecked
exceptions. Examples include ArithmeticException, NullPointerException,
and ArrayIndexOutOfBoundsException. Unchecked exceptions are not
checked at compile-time but rather at runtime by JVM. They do not need to be
explicitly caught or declared.
2. User-Defined Exceptions
User-defined exceptions are also known as custom exceptions derived from
the Exception class from java.lang package(Java package). The user
creates these exceptions according to different situations. Such exceptions
are handled using five keywords: try, catch, throw, throws, and finally.
We'll learn how to use these keywords in the Exception Handling Keywords
in Java sectionbelow.
Errors are of Unchecked type Exceptions can be both checked and unchecked.
catch specifies the code block to be executed if an exception occurs in the try block.
finally the finally block will always be executed whether an exception occurs or not
. try
A try block consists of all the doubtful statements that can throw
exceptions.
A try block cannot be executed on itself; it requires at least
one catch block or finally block.
If an exception occurs, the control flows from the try-to-catch block.
When an exception occurs in a try block, the appropriate exception
object is redirected to the catch block. This catch block handles the
exception according to its statements and continues the execution.
Syntax
try
{
//Doubtful Statements.
}
2. catch
The catch block handles the exception raised in the try block.
The catch block or blocks follow every try block.
The catch block catches the thrown exception as its parameter and
executes the statements inside it.
The declared exception must be the parent class exception, the
generated exception type in the exception class hierarchy, or a user-
defined exception.
Syntax
try
{
// code
}
catch(Exception e)
{
// code to handle exceptions
}
In the above code, we have put the "int divideByZero=5/0" in the try
block because this statement must not be executed if the denominator
is 0. If the denominator is 0, the statements after this statement in the
try block are skipped. The catch block catches the thrown exception as
its parameter and executes the statements inside it.
Output
ArithmeticException => / by zero
3. finally
The finally block in Java always executes even if there are no exceptions.
This is an optional block. It is used to execute important statements such as
closing statements, releasing resources, and releasing memory. There could
be one final block for every try block. This finally block executes after
the try...catch block.
Syntax
try
{
//code
}
catch (ExceptionType1 e1)
{
// catch block
}
finally
{
// finally block always executes
}
class Main
{
public static void main(String[] args)
{
try
{
// code that generates exception
int divideByZero = 5 / 0;
}
catch (ArithmeticException e)
{
System.out.println("ArithmeticException => " + e.getMessage());
}
finally
{
System.out.println("This is the finally block");
}
}
}
Run Code >>
In this Java example, trying to divide by zero results in
an ArithmeticException that is caught and accompanied by an error
message. The "finally" block also always runs, printing "This is the finally
block" whether or not an exception was raised.
Output
ArithmeticException => / by zero
This is the finally block