Wednesday, 14 August 2013

Exception Handling In Java

An exception is an unwanted condition that arises during the execution of a program.An exception can occur for many different reasons.Some reasons are:

  • When the user has entered invalid data.
  • When a file that needs to be opened cannot be found.
Exception handling



All exception classes are sub types of the java.lang.Exception class. The exception class is a subclass of the Throwable class. During the program execution if any error occurs and you want to print your own message then you write the part of the program which generate the error in thetry{} block and catch the errors using catch() block.The syntax is:

try
{

}
catch(ExceptionName obj)
{
   
}

There are three types of exceptions in Java. These are -:

1. Compile time Exceptions.
2. Logical Exceptions.
3. Runtime exception.

Two types of exception functions used in java:

Java Built in exceptions: Java built functions are those function that are in java.lang class.For ex:

ArithmeticException: Arithmetic error, such as divide-by-zero.
ArrayIndexOutOfBoundsException: Array index is out-of-bounds.

User defined exceptions:It can be achieved by inheriting the root exception class.

Like the Post? Do share with your Friends.

No comments:

Post a Comment

IconIconIconFollow Me on Pinterest

Blogger news

Blogroll

What's Hot