Java Exception Handling

 The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

What is Exception in Java

Dictionary Meaning: Exception is an abnormal condition.

In Java, an exception is an event that disrupts the normal flow of the program. 

Types of Java Exceptions

 

There are mainly two types of exceptions: checked and unchecked.  According to Oracle, there are three types of exceptions:

 

  1. Checked Exception
  2. Unchecked Exception
  3. Error

 

1) Checked Exception

 

 Checked exceptions are checked at compile-time.e.g.IOException, SQLException etc.


Checked Exception or compile time List:

Exception

IOException

FileNotFoundException

ParseException

ClassNotFoundException

CloneNotSupportedException


 

2) Unchecked Exception

 

 Unchecked exceptions are not checked at compile-time, but they are checked at runtime.e.g.ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException etc.


Unchecked Exception or runtime List:

ArrayIndexOutOfBoundsException

IllegalArgumentException

NullPointerException

NumberFormatException

 

3) Error

 

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.


Java Exception Keyword


There are 5 keywords which are used in handling exceptions in Java.

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. It means, we can't use try block alone.


catch


The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. 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. It is always used with method signature.




Post a Comment

1 Comments

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)