Friday, 16 January 2015

Types of Exception in java



Predefined exceptions are divided into two types. They are synchronous exceptions and asynchronous exceptions

Asynchronous exceptions are those which are always deals with hardware problems
Ex: java.lang.Error
Synchronous exceptions are one which always deals with programmatic errors.
          Ex:  java.lang.Exception

Checked Exceptions:  deals with compile  time errors regarding class not found and interface not found

Unchecked exceptions: are those which are always deals with programmatic run time errors
ArithmeticException, NumberFormatException, ArrayIndexOutOfBoundsException




Try block:
1. This is the block in which we write the block of statements which are to be monitored by JVM at run time i.e., try block must contain those statements which causes problems at run time.
2. If any exception is taking place the control will be jumped automatically to appropriate catch
block.
3. If any exception is taking place in try block execution will be terminated and the rest of the statements in try block will not be executed  at all and the cont
4. For every try block we must have at least one catch block. It is highly recommended to write ‘n’ number of catch’s for ‘n’ number of problematic statements.


Catch block:
1. This is used for providing user friendly messages by catching system error messages.
2. In the catch we must declare an object of the appropriate execution class and it will be internally referenced JVM whenever the appropriate situation taking place.
3. If we write ‘n’ number of catch’s as a part of JAVA program then only one catch will be executing at any point.
4. After executing appropriate catch block even if we use return statement in the catch block the
control never goes to try block.


Finally block:
1. This is the block which is executing compulsory whether the exception is taking place or not.
2. This block contains same statements which releases the resources which are obtained in try
block (resources are opening files, opening databases, etc.).
3. Writing the finally block is optional

Number of ways to find details of the exception:
1.Using an object of java.lang.Exception
2. Using printStackTrace method
3. Using getMessage method


Using an object of java.lang.Exception
An object of Exception class prints the name of the exception and nature of the message
try
{
int x=Integer.parseInt ("10x");
}
catch (Exception e)
{
System.out.println (e); // java.lang.NumberFormatException : for input string 10x
}
java.lang.NumberFormatException is  name of the exception

for input string 10x- nature of the message


2. Using printStackTrace method:
This is the method which is defined in java.lang.Throwable class
and it is inherited into java.lang.Error class and java.lang.Exception class.
This method will display name of the exception, nature of the message and line number where the exception has
taken place.
For example:
try
{
......;
int x=10/0;
......;
}
catch (Exception e)
{
e.printStackTrace (); // java.lang.ArithmeticException : / by zero : at line no: 4
}

java.lang.ArithmeticException-  name of the exception

/ by zero- nature of the message

  at line no: 4  -  line number

3.Using getMessage method:
This is also a method which is defined in java.lang.Throwable class and it is inherited into both Error and Exception classes. This method will display only nature of the message.

For example:
try
{
......;
int x=10/0;
......;
}
catch (Exception e)
{
System.out.println (e.getMessage ()); // / by zero
}

No comments:

Post a Comment