Exception Handling

Manish singh rav
4 min readMay 16, 2021

In this article we’ll go through the concept of exceptions and how to handle an exception using java programming language.

In this article we’re going to know that how to handle an exception, but before that, firstly we need to understand that “what is an exception ?”

Exception :

We can say that an exception is a mechanism which disturbs the normal flow of a program. when an exception occurs system will give us a message about that exception, but a good thing about an exception is that exception can be handled in java.

If an exception occurs and programmer will not handle it, than execution of the program will terminate and an error message will print to the display.

Let’s understand this with an example .

So we created a public class Main, in this class we have main method inside that method, we created two variable and name them i and j and initialize i with 0 and j with 15 and printing that j is divided by i . because of this it will generate an exception which divided by zero exception as mention below.

exception occurred by program.

Hierarchy of java exception classes :

Difference between error and exception :

Error and exception both are subclass of throwable class, let’s understand difference between error and exception.

Error :

Errors are usually raised by the environment in which the application is running. For example, an error will occur due to a lack of system resources.

→It is not possible to recover from an error.

→Errors occur at run-time and are not known by the compiler.

→“OutOfMemory” and “StackOverflow” are examples of errors.

Exception :

→Exceptions occurs by the code of the application itself.

→Using “ try-catch ” blocks we can handle exceptions and recover the application from them.

→Exceptions can be checked or unchecked.

→“IndexOutOfBounds” is an example of exception.

Type of exceptions in java :

There are two types of exceptions, “Checked Exception and Unchecked Exception”.

Checked Exceptions :

→Checked exceptions are exception that are known to the compiler.

→Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not.

→These are the few checked exceptions: → SQLException, IOException, ClassNotFoundException etc.

Unchecked Exceptions :

→These exceptions are not checked at compile-time so compiler doesn’t check whether the programmer has handled them or not.

→It is the responsibility of the programmer to handle these exceptions.

→These are the few unchecked exceptions: →ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

Keywords used for handle the exceptions in java :

There are few keywords which we use to handle the exceptions.

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 exception and we use catch block after try block.

throw : → The “throw” keyword is used to throw an exception.

finally :→The “finally” block is used to execute the important code of the program, it will execute not matter whether the exception is handled or not.

Let’s use these keywords to handle the exception, as we see the code which is giving the divide by zero exception, let’s try to handle that .

by using this we can handle the exception.

Let’s use another example and try to handle the exception in that .

If we don’t use try and catch it will throw an exception “ArrayIndexOutOfBoundsException”

Finally block is execute no matter if exception is handled or not.

--

--