Java Exception Handling try- catch

Although the default exception handler provided by the java run-time system is useful for debugging ,you will usually want to handle an exception yourself.Doing so provides two benefits .First it allows you to fix the error.Second it prevents the program from automatically terminating .Most users would be confused if your program stopped running and printed a stack trace whenever an error occurred !Fortunately its quite easy to prevent this .
To guard against and handle a run time error ,Simply enclose the code that you want to monitor inside a try block an a catch clause which processes the  Arithmetic Exception generated by the division -by-zero error


class tryexample
{
  public static void main(String arg[])
    {
      int a,d;
      try
        {
            d=0;
        a=42/d;
        System.out.println("Output :"+a);
            }
      catch(ArithmeticException e)
       {
          System.out.println("Division is not possible "+e);
        }
    }
}


Output

Division is not possible java.lang.ArithemeticException : / by Zero


Java Exception Handling try- catch

0 comments:

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org