Exceptions and Exception Handling in Java

Hi folks, hope you are doing good, If you are just started with java programming language, then keep your close attention in this post as i will explain very important topic “Exceptions and exception handling in Java”.

There are hierarchy in java to handling the exceptions during your program compilation and run time. We are using the try-catch blocks to catching an exception in program.

What is the exception?

Exception are the events that arises during program execution and disrupt the normal flow of program instructions. It is very powerful mechanism to handle the exception to maintain the flow of your java programs.

Types of Exceptions in Java

There are two types of exceptions in java programming language.

1) Checked Exceptions
2) Un-checked Exceptions

1) Checked Exceptions

There are three checked exception such as SQLException, ClassNotFoundException and DataAccessExeception.

2) Un-checked Exceptions

There are four un-checked exception such as RunTimeException, AirthmaticException, NullPointerExeception and DOMExeception.

Exception Hierarchy

Below diagram will help you to make more understand of java exceptions.

exception handling in java diagram

Behavior of try-catch Blocks

First, statements in the try block are executed. If not thrown any exception by the try block, then compiler passes to the next statement. Second, If an exception thrown within try block, then catch block will executed to catch the exception. Below is simple example of try-catch like:

Below is the complete java class with try-catch block:

Hope this tutorial, helps you to make more understand of exceptions and exception handling in Java. Do like & share this tutorial with your friends and on social media.

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts:

Article Tags: , , , , ,