Sitemap

Exception Handling in Java

2 min readMar 24, 2022

--

24MAR,2022

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

In this tutorial, we will learn about Java exceptions, it’s types, and the difference between checked and unchecked exceptions.

What is Exception in Java?

Dictionary Meaning: Exception is an abnormal condition.

Get Riturajbelwal’s stories in your inbox

Join Medium for free to get updates from this writer.

In Java, an 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 ClassNotFoundException, IOException, SQLException, RemoteException, etc.

Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application; that is why we need to handle exceptions.

Press enter or click to view image in full size

Types of Java Exceptions

There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely:

  1. Checked Exception
  2. Unchecked Exception
  3. Error

JavaExceptionExample.java

  1. public class JavaExceptionExample{
  2. public static void main(String args[]){
  3. try{
  4. //code that may raise exception
  5. int data=100/0;
  6. }catch(ArithmeticException e){System.out.println(e);}
  7. //rest code of the program
  8. System.out.println(“rest of the code…”);
  9. }
  10. }

Test it Now

Output:

Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...

--

--