Search This Blog

Showing posts with label Exception handling. Show all posts
Showing posts with label Exception handling. Show all posts

27 June, 2011

Exception handling: Java specific part-5



Part- 5
Catch all exception handler
• Since all exceptions are subclasses of
Exception,
– To catch all exceptions, one uses the
catch-all exception handler with
Exception as the type
catch (Exception e) {
//catch block
}
• Since one exception is handled by at most one
catch block,
– if there is more than one catch block,
– and a catch-all,
• then the catch-all error handler must appear last
– for the first catch block which fits the
exception will be executed.
• Similarly, if there are two catch blocks one
involving a base type and one a derived type,
– then the catch block with base type must
appear last
– since a base type will fit a derived type,
but not vice versa.



Exceptions: Miscellaneous
• Try blocks may be nested.
– In this case, if the first try block does not
have an appropriate handler for the
exception, the exception will be passed
to the outer block, and so on.
– The exception can be handled by both
inner and outer catch block, by making
the inner catch block rethrow what it has
caught.
• A finally clause may be associated with each try
block, and this is guaranteed to execute.
– The JVM achieves this by checking all
possible exits to the try block.


Program:-


import java.io.*;
public class NestedCatch
{
public static void main(String args[])
{
int a;
int b;
try
{
try
{
System.out.println (“a = ”);
DataInputStream in = new
DataInputStream(System.in);
String inString = in.readLine();
a = Integer.parseInt (inString);
System.out.println (“b = ”);
b = Integer.parseInt (in.readLine());
int c = a/b;
}
catch(ArithmeticException e)
{
System.out.println (“Caught Arithmetic
exception”);
throw e; //rethrow
}


catch (IOException i)
{
System.out.println (“Caught i/o
exception”);
}
finally
{
System.out.println (“Exiting inner try”);
}
}
catch(Exception e)
{
System.out.println (“Caught something”);
}
System.out.println (“Program exit”);
}
}


• input:
a=2
b=2
• output:
Exiting inner try
Program exit
• input:
a=*
• output:
Exiting inner try
Caught something
Program exit


-------------- <<<Part- 4 ----------------------------------



Exception handling: Java specific part-4


Part - 4
Exceptions: Throwable and checked exceptions
• Unlike C++, primitive types cannot be thrown.
Throwable types must be subclasses of
Throwable
• In the previous example we saw that an
uncaught exception leads to program
termination.
• Not all exceptions can be uncaught. Exceptions
may be divided into two classes
• Unchecked exceptions are
– subclasses of Error, or
– subclasses of RunTimeException
• checked exceptions: all others
Exceptions: catch all
• if a method can generate checked exceptions,
then these must be either
– specified using a throws clause
– or caught in a try block
else there is a compile-time error
• Unchecked exceptions need not be specified.
– hence lazy programmers tend to
subclass RunTimeException to produce
their exceptions.


Program:-

class MyException extends Exception
{
int exception_num;
}
public class CheckedException
{
int myMethod (int a, int b) throws
MyException
{
MyException e = new MyException();
if (a==0)
{
e.exception_num = 1;
throw e;
}
if (b==0)
{
e.exception_num=2;
throw e;
}
return a/b;}
public static void main (String args[])
{
CheckedException e = new
CheckedException();
try
{
e.myMethod (0, 1);
}

catch (MyException me)
{
System.out.println (me.exception_num);
}
}
}
• The above program will NOT compile if
– the throw clause is removed from
myMethod, or
– the catch clause is removed from main
(and no throws clause is provided to
main)


<<<-Part - 3     ------------------------   Part - 5 >>>   


Exception handling: Java specific part-3


Part - 3
Exceptions: multiple catch blocks
• Catch is not called: program execution is
transferred to it.
– if no exception occurs, then the catch
block will NOT execute.
• There may be more than one catch block.
– Each catch block handles one type of
exception .
– But only that catch block will be
executed which matches the type of the
exception generated.
• Thus, if we rewrite the previous program, so that
– there are two catch blocks:
– one which handles arithmetic exceptions.
– and one which handles i/o exceptions,
– and if an integer is incorrectly entered,
– then the exception will NOT be caught,
and an abnormal program termination
will occur .


Program:-

import java.io.*;
public class MultipleCatch
{
public static void main(String args[])
{ int a;
int b;
try
{
System.out.println (“a = ”);
DataInputStream in = new
DataInputStream(System.in);
String inString = in.readLine();
a = Integer.parseInt (inString);
System.out.println (“b = ”);
b = Integer.parseInt (in.readLine());
int c = a/b;
}
catch(ArithmeticException e)
{
System.out.println (“Caught Arithmetic"+
“exception”);
}
catch (IOException i)
{ System.out.println (“Caught i/o”+
“exception”);
}
System.out.println (“Program exit”);
}
}




• Case 1: input:
a=2
b=3
• output: Program exit
• Case 2: Inputa=2
b=0
• Output:
Caught Arithmetic Exception
Program exit
• Case 3: input: a= *
• output:
java.lang.NumberFormatException: *
at java.lang.Integer.parseInt(Integer.java:231)
at java.lang.Integer.parseInt (Integer.java:278)
at MultipleCatch.main (MultipleCatch.java:15)
• Case1: no catch block is executed, since no
exception is thrown.
• Case 2: the first catch block is executed, since
an ArithmeticException is thrown.
• Case 3: a NumberFormatException is thrown but
there is no matching catch block. Hence the
program terminates.


<<<-Part - 2      ------------------------   Part - 4 >>>    


Exception handling: Java specific part-2


Part - 2




Exceptions: try, throw, catch sequence


• The following code is an example of a basic
try-throw-catch sequence.


public class CatchException
{
public static void main(String args[])
{
int a = 1;
int b = 0;
try //start try block
{
System.out.println (“Trying...”);
int c = a/b; //exception is thrown
}
catch (ArithmeticException e)//and caught
{
System.out.println (“Well caught”);
}
System.out.println (“Normal program exit”);
}
}


• Output: Trying...
Well caught
Normal program exit.


<<<-Part - 1      ------------------------   Part - 3 >>>                                                        

Exception handling: Java specific part-1


Part - 1
In Java exception handling is done through 5 keywords.


– throw
– try
– catch
– throws
– finally


• When an exception is thrown,


– not only is the stack unwound to a
previously saved state, but
– automatic objects are marked for
finalization (destructors are exeecuted in
C++)
– if the exception is thrown from within a
constructor, the finalizer of that object is
not executed.
– key resources may be manually freed
through the use of a finally block which is
guaranteed to execute, whether or not an
exception is thrown.


• The code to be monitored is put into a a try block.


• The action to be taken, when an exception is
thrown is put into various catch blocks.



• Each catch block decides what action is to be
taken when a particular exception is thrown. try{//try block
}
catch (Exception_type1 arg)
{ //catch block
}
catch (Exceptiontype2 arg)
{ //catch block 2
}
.............................................      Part - 2   .....................................

Meetme@