An exception is an abnormal condition that has occurred in a piece of code. It may be refered to as error.
The java Exception is an object that describes an exceptional condition that has occurred in a piece of code. When an exceptional condition arises , an object representing that exception is caught and thrown in the method that caused it.
The exception can be generated by the java run-time system or they can be manually generated by a code.
In Java , the exception is managed by 5 keywords :
- try
- catch
- throw
- throws
- finally
The program statements that you want to check are contained within the try block . If an exception occurs within the try block ,it is thrown.
Your code can catch the exception using catch and handle it in some rational manner. To manually throw an exception , use throw.
Any exception that is thrown out of the must be specified by a throws clause.
Any code that must be executed before a method returns is put in a finally block.
It's general form is :
try {
//block of code to monitor for errors
}
catch ( ExceptionType 1 e)
{
//Exception handler
}
.....
catch ( ExceptionType N e)
{
//Exception handler
}
finally
{
//Code that must be executed
}
The exception that is not caught by your program will ultimately be processed by the default handler. The default handler displays a string describing the exception , prints a stack trace from the point at which the exception ocurred and terminates the program.
Ex- ( Try and Catch )
class TryCatchDemo
{
public static void main ( String [] args )
{
int d,a;
try
{
d=0;
a=42/d;
System.out.println (" This will not be printed");
}
catch ( ArithmeticException e)
{
System.out.println (" Caught " + e);
}
System.out.println (" After catch statement");
}
}
Output:
Division by Zero
After Catch Statement
Ex- ( Multiple catch clauses)
class MultipleCatch
{
public static void main ( String [] args )
{
try
{
int a=args.length;
System.out.println( "a=" +a);
int b =42/a;
int c[] = {2};
c[42]=99;
}
catch ( ArithmeticException e)
{
System.out.println (" Divide by 0 " + e);
}
catch ( ArrayIndexOutOfBoundsException e)
{
System.out.println( " Array index oob :" + e);
}
System.out.println ( " After try/catch blocks ");
}
}
Ex- (Nested Try )
class NestedTry
{
public static void main ( String [] args )
{
try
{
int a= args.length;
int b = 42/a;
System.out.println ( "a = " + a);
try
{
if (a==1)
a=a/(a-a);
if ( a==2)
{
int c[] = {1};
c[42]=99;
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(" Array index out of bounds" +e);
}
}
catch (ArithmeticException e)
{
System.out.println (" Divide by 0 : " + e);
}
}
}
Ex- ( Throw )
class throwdemo
{
static void demo ()
{
try
{
throw new NullPointerException("d");
}
catch( NullPointerException e)
{
System.out.println (" caught inside ");
throw e;
}
}
public static void main ( String [] args )
{
try
{
demo ( );
}
catch( NullPointerException e)
{
System.out.println(" Recaught :" + e);
}
}
}
Ex- ( Throws)
class ThrowsDemo
{
static void throwOne () throws IllegalAccessException
{
System.out.println(" Inside throw one");
throw new IllegalAccessException ("demo");
}
public static void main ( String [] args )
{
try
{
throwOne( );
}
catch ( IllegalAccessException e)
{
System.out.println ( "Caught " +e);
}
}
}
Ex- ( Finally )
class FinallyDemo
{
public static void main ( String [] args )
{
int d,a;
try
{
d=0;
a=42/d;
System.out.println (" This will not be printed");
}
catch ( ArithmeticException e)
{
System.out.println (" Caught " + e);
}
finally
{
System.out.println (" After catch statement");
}
}
}
By: Knowledge Bits
No comments:
Post a Comment