What is an Exception in Java and types of exceptions

What is an Exception ?

Let’s say you have a program that takes input from the user and finds out the reminder of 100/(user-input-number).

take the number input from the user

find out the reminder by dividing 100 with that number

print the result

e.g. if user entered ‘9’, the program will print 1 ( 9*11= 99 , so reminder is 1). But what will happen if user entered ‘0’ ? Dividing a number by ‘0’ is not possible. So, it will throw an error and stops the execution of the program. These types of error that occurs during execution time is called an Exception . As mentioned on Oracle doc, “An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.”

Handling an Exception :

We can handle an exception manually. We should always handle an exception because if our program knows that an exception occurred , it can show an message to the user instead of terminate directly. That piece of code which handles an exception is called ‘Exception Handler’. If any exception is thrown in a method, first the runtime system checks if that method contains any exception handler. If not, it will move to the method that was calling this method i.e. it moves in reverse order the method was called. If any exception handler is found, it will pass the exception object to that handler. Exception handlers are also different types. It will process an exception only if it can execute it. Otherwise it will return back the exception. If no exception handler is found, the program will terminate.

Kind of Exceptions in Java :

We have three different types of exception in Java.They are categorized in two groups : Checked Exceptions and Unchecked exceptions. Let’s take a look into them one by one :

Unchecked Exception :

Unchecked exceptions are of two types : Errors and Runtime Exception.

Errors :

Your application doesn’t produce errors. They are mainly caused by the environment in which the JVM is running. For example, you want to read from a file. Your program opens the file successfully but it is unable to read because the read permission for that file is not available. These types of exceptions cannot be recovered. But you can handle the exception and show one message to the user that the read permission is not available for the file.

Runtime Exception :

These types of exception occurs normally because of programming bugs or logical errors. Suppose you have one method, you pass a specific type of object to it and read some data. If for some reason, one ‘null’ object is passed, it will throw an exception known as NullPointerException. You can catch an exception like this and print some message.

Checked Exception :

Checked exceptions are recoverable. For example, you have an application that reads contents of a file and the name of the file should be given by the user. If for some user given file name the file doesn’t exist, it will throw an exception. You can handle that exception and since you know that the problem was with the user input, you can prompt the user to input the filename one more time.

Similar tutorials :