Exception And Exception Handling In Java

Tusharchavhan
3 min readMay 16, 2021

In this article, you learned about Exceptions and different ways to handle those exceptions.

Fig:- Reference Image For Exception In Java

Exception:-

Dictionary Meaning: Exception is an abnormal condition.

When executing Java code, different errors can occur: coding errors made by the programmer, errors due to wrong input, and when this error occurs Java program will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error).

“An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.

Hierarchy of Java Exception:-

Fig:- Reference Image For Hierarchy Of Java Exception

Types Of Exception:-

There are 3 types of Exception present in java

  1. Checked Exception
  2. Unchecked Exception
  3. Error

1. Checked Exception:-

Checked Exception is checked at compile time these are also called Compile Time Exception. That exception is handled by using the try-catch block throws keyword.

2. Unchecked Exception:-

Unchecked Exception occurs at the time of execution. This exception is also called Runtime Exception.

3. Error:-

An Error indicates a serious problem. An error can not be recoverable eg. StackOverFlowError we can not handle this error.

Fig:-Reference image for types of exception.

Some Examples Of Predefined Exception In Java:-

Please refer to the following image for exception present in java.

Exception Handling In Java:-

When we required a smooth execution flow of programs we handled the occurring exception. To handle the exception Java provides some keyword these are followed,

  1. try

2. catch

3. finally

4. throws

5. throw

1. try:-

The try keyword is basically used to declare try block where we write exception code means that code which throws an exception. The try block always comes with a catch block or finally block means we can not write try block alone.

2. catch:-

The catch keyword is basically used to declare catch block we write handling code in a catch block. We can declare multiple catch blocks for one try block. We can’t write catch block alone means catch block always comes with a try block.

3. finally:-

The finally keyword is used to declare the finally block in which we write our important code in the finally block. The finally block always comes try block. We cant write finally block alone, finally blocks always execute.

Fig:- Sequence of Writing try-catch and finally block

4. throw:-

The throw keyword is used to throw an exception.Also we can

Fig:-Image shows for syntax of throw keyword

5. throws:-

The throws keyword is used declare an exception. By using throws keyword, we can’t handle any exception. Also throws keyword can’t throw any exception. It is declared in the definition of method. Also throws keyword is used for exception propagation.

Fig:-Syntax for throws keyword

--

--