Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Exception Handling in Java

In Java, an Exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

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

An exception can occur for many different reasons. Following are some scenarios where an exception occurs.

  • A user has entered an invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

Advantage of Exception Handling

An exception normally disrupts the normal flow of the application that is why we use exception handling. Let’s take a scenario:

                                Statement 1;

                                statement 2;

                                statement 3; //exception occurs

                                statement 4;

                                statement 5;

                                statement 6;

                                statement 7;

                                statement 8;

                                statement 9;

                                statement 10;

Assume that, there are 10 lines in our program, and the exception came at the 3rd line of our program, if we have not handle the exception, then the program abnormally terminated and from the line 4 to line 10 will not be executed.

If we handle the exception, the JVM will take care of exception and the remaining lines will be executed completely.

Hierarchy of Java Exception classes

The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error.



This post first appeared on Technology Career Development Center, please read the originial post: here

Share the post

Exception Handling in Java

×

Subscribe to Technology Career Development Center

Get updates delivered right to your inbox!

Thank you for your subscription

×