Managing exceptions and errors

Types of errors

—     Compile time errors

—     Run time errors

 

Compile time errors

—   All syntax errors

—   Wont create .class file

—   Most common problems are:

Missing semicolons

Missing brackets in classes and methods

Misspelling of identifiers and keywords

Missing double quotes in string

And so on..

 

Run time errors

—   Will create .class file but may not run properly

—   Most common errors are:

dividing by zero,

referencing missing files,

 calling invalid functions,

 not handling certain input correctly

 Run time errors




—    Program is syntactically correct

—    While executing, an abnormal termination occurs

Exception      

—    An exception is a condition that is caused by a runtime error in the program

—    Java interpreter creates an exception object and throws

—    If the exception object is not caught and handled properly, an error occurs and an abnormal termination occurs

Exception handling

    it is a mechanism provided as a means to detect and report an ‘exceptional circumstances’ so that the appropriate action can be taken. Tasks to be followed in Exception handling are:


1.               Find the problem(Hit the exception)

2.               Inform that an error has occurred(Throw the exception)

3.               Receive the error information(Catch the exception)

4.               Take corrective actions(Handle the exception)

 

Common Java Exceptions



Syntax of exception handling




















—     Java uses a keyword try to preface a block of code that is likely to cause an error

—     Whenever such error occurs, it throws

—     A catch block defined by the keyword catch catches the exception thrown

—     Try block can have more than 1 statements

—     If one statement generate exception, remaining statement in the block are skipped                    


Program


Class Error

{
      public static void main(String args[])

      {

      int a=10,b=5,c=5,x,y;

      try

                  {
                  x=a/(b-c);//Exception here

                  }
      catch(ArithmeticException e)

                  {

                  System.out.println(“division by zero “);

                  }

      y=a/(b+c);

      System.out.println(”y=”+y);

      }

}
     

Output

Division by zero

y=1

 

Multiple catch statement

……….

try

{

            statement;

}

Catch(Exception-type1 e)

{

Statement;

}

Catch(Exception-type2 e)

{

Statement;

}

............

............

............

Catch(Exception-type1 e)

{

Statement;

}

 

Using finally statement

—     Used to handle an exception that is not caught by any of the previous catch blocks

—     It may be added immediately after the try statement or after the last catch block

—     The statements in the finally block will execute regardless of whether or not exception is thrown

 Syntax

try

{

            --------

            --------

}

finally

            {

                        ………..

                        ………..

            }

 

Or

 

try

{

            --------

            --------

}

Catch (….)

            {

                        ……..

                        ………

            }

Catch (…..)

            {

                        ……..

                        ………

            }

Catch (…..)

            {

                        ……..

                        ………

            }

............

............

............


finally

            {

                        ………..

                        ………..

            }

 

Throwing our own exception

Can be done by using the keyword throw

Syntax:

throw new Throwable_subclass;

Eg: throw new ArithmeticException();

—  can use the userdefined subclass of throwable class

—  Exception is the subclass of throwable

 

 

program





 











Output

        Caught my exception

        Number is too small

        I am always here


Program

Class Error

{
            public static void main(String args[])

            {

            int a,b,c;

            int x,y;

             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

            try

            {

             System.out.println("Enter 3 numbers");

             a=Integer.parseInt(br.readLine());

             b=Integer.parseInt(br.readLine());

             c=Integer.parseInt(br.readLine());
            x=a/(b-c);//Exception here if b and c are same

            }
            catch(ArithmeticException e)

            {

            System.out.println(“division by zero “);

            }

            y=a/(b+c);

            System.out.println(”y=”+y);

            }

}

 

No comments:

Post a Comment