Search This Blog

27 June, 2011

Exceptions


• An exception is an unusual condition in a
program.
– Example: an illegal mathematical
operation is an exception.
– Eg. integer division by zero will lead to
an exception and to immediate program
termination.


Program :-
public class DivideByZero
{
public static void main (String args[])
{
int a=1;
int b=0;
System.out.println (“a/b = ” + a/b);
}
}


• output:
java.lang.ArithmeticException: / by
zero
at
DivideByZero.main(DivideByZero.java:7)
Application Exit



• Though the stack trace provided by Java is
helpful to the programmer for debugging, such
abrupt termination is likely to leave the user
confused.
• Exceptions allow the programmer to handle such
situations smoothly..


• The classical C approach to exception handling
involves two routes.
– signal and raise
– setjmp and longjmp


• In the signal method, an exceptional condition,
such as division by zero, is indicated by means
of a signal SIGFPE, defined in signal.h
• (Alternatively, a user may raise a signal through
raise.).
• When a signal is raised, a signal handler (a
function) is called.
• A default signal handler is provided, and the user
may override it by registering an alternative
signal handler (pointer to a function).
• A default math error handler is provided, and the
user may override it by redefining the function
matherr.


---------------------------------------


Need of Exception Handling


---------------------------------------

No comments:

Post a Comment

Meetme@