Exception

An exception is an abnormal condition that arises in a code sequence at run time.These have to handle manually or by the error code which takes time and trouble. Java avoids these problems by providing Exception Handling Mechanism.

Fundamentals of Exception-Handling

  • A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code.
  • Java exception handling is managed via five keywords: try, catch, throw, throws, and finally.
  • try- Program statements that we want to monitor for exceptions are contained within this block.
  • catch-Our code can catch this exception (using catch) and handle it in some rational manner.
  • throw- To manually throw an exception, use the keyword throw however system generated exceptions are automatically thrown by java run-time system.
  • throws-Any exception that is thrown out of a method must be specified as such by a throws clause.
  • finally-Any code that absolutely must be executed after a try block completes is put in a finally block.

    The general form of an exception-handling block:

try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
// ...
finally
{
// block of code to be executed after try block ends
}

Exception Types

  • All exception types are sub classes of the built-in class Throwable.
  • Throwable are two sub classes that partition exceptions into two distinct branches.
  • One branch is headed by Exception.This class is used for exceptional conditions that user programs should catch.

  • There is an important sub class of Exception, called RuntimeException which are automatically defined for the programs that you write and include things such as division by zero and invalid array indexing.

  • The second branch is Error,that are not expected to be caught under normal circumstances by your program.Stack overflow is an example of such an error.

    The top-level exception hierarchy::

Uncaught Exceptions

When we doesn't handle the exception

//example for uncaught exceptions
class Exc0 
{
 public static void main(String args[]) 
 {
 int d = 0;
 int a = 42 / d;  //program will not execute
 }
}

Output:

java.lang.ArithmeticException: / by zero at Exc0.main(Exc0.java:4)

  • When the Java run-time system detects the attempt to divide by zero, it constructs a new exception object and then throws this exception.
  • Once exception has been thrown it must be handle, As in this program there is no handler for this exception so it will handle by the default handler by java runt-time system.

The stack trace will always show the sequence of method invocations that led up to the error.

//example for exception rises in a particular method
Public class Exc1
{
static void subroutine()
{
int d = 0; int a = 10 / d; // at this particular method it will shows error
}
 public static void main(String args[])
{
Exc1.subroutine();
}
}

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero+

at Exc1.subroutine(Exc1.java:6)

at Exc1.main(Exc1.java:10)

results matching ""

    No results matching ""