This article teaches what exception handling in your C
Exception handling in java is one of powerful mechanism to
handle run time errors so that normal flow of the application can be
If you want to know about Exception handling in details then you learn first what is exception so in this post I’ll teach you both what is exception and what exception handling.
What is exception:
Exception is abnormal condition, In java – exception is an
event that disrupts the normal flow of the program. It is an object which is
thrown at runtime.
What is exception Handling:
Exception Handling is a mechanism to handle runtime errors
such as ClassNotFound, IO, SQL, Remote etc.
Advantage of Exception Handling:
The core advantage of exception handling is to maintain the
normal flow of the application. Exception normally disrupts the normal flow of
the application that is why we use exception handling. Let's take a scenario:
Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the exception will be executed. That is why we use exception handling in java.
Hierarchy of java Exception classes:
Types of Exception:
There are mainly two
types of exception: checked and unchecked where error is considered as
Difference between checked and unchecked exceptions:
Checked Exception: The
classes that extend Throwable class except RuntimeException and error are known
as checked exceptions IOException,
SQLException etc. checked exception are checked at compile time.
Unchecked Exception: The classes that extend RuntimeException are known as unchecked exceptions ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. unchecked exceptions are not checked at compile-time rather they are checked at runtime.
Scenario where ArithmeticException occurs:
If we divide any number by zero there occurs an
Scenario where NullPointerException occurs:
If we have null value in any variable, performing any operation by
the variable occurs an NullPointerException.
Scenario where NumberFormatException occurs:
The wrong formatting of any value, may occur
NumberFormatException. Suppose I have a string variable that have characters,
converting this variable into digit will occur NumberFormatException.
Scenario where ArrayIndexOutOfBoundsException occurs:
If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
Java Exception Handling Keywords:
There are 5 keywords used in java exception handling.
Check more posts on exception handling here