Search This Blog

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

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


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

Why in java - Need of Exception Handling


class ppp
{
public static void main(String ff[])
{
int a, b, c;
a = 10;
b= 0;
c=a/b;
/*Code termination because Arithmetic Exception of divide by zero*/

System.out.println(c);

//Some lines of code

System.out.println("program exits");
}  // Neat End
}


Output:- Exception in thread "main" java.lang.ArithmeticException: / by Zero. 

Note:- Thus to provide successful or Neat End to this program. We need to handle Exception or can say need of Exception handling topic and to do it we have to understand why exception is generation in code.

------------- Continue ->  Exception Handling -------------------------

Meetme@