Exception Handling in Java
Exception Handling in Java
The Exception Handling in Java is one of the powerful mechanisms to handle the
runtime errors such as IOException, ClassNotFoundException, SQLException,
RemoteException, etc. so that normal flow of the application can be maintained.
Exception is an abnormal condition. In Java, an exception is an event that disrupts the
normal flow of the program. It is an object which is thrown at runtime. The advantage
of exception handling is to maintain the normal flow of the application.
Suppose there are 10 statements in program and there occurs an exception at statement
5, the rest of the code will not be executed. If we perform exception handling, the rest of
the statement will be executed. That is why we use exception handling in Java.
1. Types of Java Exceptions:- There are two types of exceptions: checked and
unchecked. Here, an error is considered as the unchecked exception. According
to Oracle, there are three types of exceptions:
Keyword Description
try The "try" keyword is used to specify a block where we should place
exception code. The try block must be followed by either catch or finally.
catch The "catch" block is used to handle the exception. It must be preceded by try
block It can be followed by finally block later.
finally The "finally" block is used to execute the important code of the program. It is
executed whether an exception is handled or not.
throw The "throw" keyword is used to throw an exception.
throws The "throws" keyword is used to declare exceptions. It doesn't throw an
exception. It specifies that there may occur an exception in the method.
3. Hierarchy of Java Exception classes:- The java.lang.Throwable class is the root
class of Java Exception hierarchy which is inherited by two subclasses: Exception
and Error. A hierarchy of Java Exception classes are given below:
4. Common Scenarios of Java Exceptions:- There are given some scenarios where
unchecked exceptions may occur. They are as follows:
Java try block is used to enclose the code that might throw an exception. It must be used
within the method. If an exception occurs at the particular statement of try block, the
rest of the block code will not execute. So, it is recommended not to keeping the code in
try block that will not throw an exception. Java try block must be followed by either
catch or finally block.
1. try{
2. //code that may throw an exception
3. }catch(Exception_class_Name ref){}
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter. The declared exception must be the parent class exception ( i.e.,
Exception) or the generated exception type. However, the good approach is to declare
the generated type of exception. The catch block must be used after the try block only.
You can use multiple catch block with a single try block.
3 Internal working of java try-catch block
The JVM firstly checks whether the exception is handled or not. If exception is not
handled, JVM provides a default exception handler that performs the following tasks:
Prints out exception description.
Prints the stack trace (Hierarchy of methods where the exception occurred).
Causes the program to terminate.
But if exception is handled by the application programmer, normal flow of the
application is maintained i.e. rest of the code is executed.
A try block can be followed by one or more catch blocks. Each catch block must contain
a different exception handler. So, for different tasks at the occurrence of different
exceptions, use java multi-catch block.
At a time only one exception occurs and at a time only one catch block is executed. And
all catch blocks must be ordered from most specific to most general, i.e. catch for
ArithmeticException must come before catch for Exception.
7. Java Nested try block:- The try block within a try block is known as nested try
block in java. Sometimes a situation may arise where a part of a block may cause
one error and the entire block itself may cause another error. In such cases,
exception handlers have to be nested.
8. Java finally block:- Java finally block is a block that is used to execute important
code such as closing connection, stream etc. Java finally block is always executed
whether exception is handled or not. Java finally block follows try or catch block.
Finally block in java can be used to put "cleanup" code such as closing a file,
closing connection etc. For each try block there can be zero or more catch blocks,
but only one finally block. The finally block will not be executed if program
exits(either by calling System.exit() or by causing a fatal error that causes the
process to abort).
Note: If you don't handle exception, before terminating the program, JVM
executes finally block(if any).
13 Java Custom Exception:- If you are creating your own Exception that is known
as custom exception or user-defined exception. Java custom exceptions are used
to customize the exception according to user need. By the help of custom
exception, you can have your own exception and message.
14 Assertion:- Assertion is a statement in java. It can be used to test your
assumptions about the program. While executing assertion, it is believed to be
true. If it fails, JVM will throw an error named AssertionError. It is mainly used
for testing purpose.
Advantage of Assertion:
It provides an effective way to detect and correct programming errors.
Syntax of using Assertion:
There are two ways to use assertion.
1. assert expression;