Java Exception Handling

 

JAVA

Exception Handling

 


Fill in the blanks

 

        1.When the java arises exception in code sequence Runtime

        2. What keywords must be used to monitor for exceptions? Try

        3.Write a keyword for manually throw an exception throw

        4.Write a keyword must be used to handle the exception thrown by try block in some rational manner? Catch

        5.Dividing a number zero is called Arithmetic Exception

       

  

Answer the Following Questions

                                                                (4 marks)

1.Write a program for Arithmetic Exception

File name: TryDemo.java

class TryDemo

{

        public static void main(String args[])

      {

                 int d=0;

                 int a;

                 try {   // monitor a block of code.

                      a= 42 / d;

                      System.out.println("This will not be printed");

              }catch (ArithmeticException e)

              {   //catch divide-by-zero error

                 System.out.println("Division by Zero");

               }

                 System.out.println("After catch statement");

            }

}

2.Write a program for Multiple Catch clauses

File name: MutipleCatch.java

class MutipleCatch {

         public static void main(String args[]) {

           try  {

                     int a =args.length;

                    System.out.println("a = " +a);

                    int b=42/a;

                    int c[] ={1};

                    c [42] = 99;

            }catch(ArithmeticException e)  {

                 System.out.println("Exception Occurred:"+e);

           }catch(ArrayIndexOutOfBoundsException e)  {

                 System.out.println ("Exception Occurred:"+e);

            }

              System.out.println("After try/catch blocks.");

          }

}

3.Write a program for throws Exception

import java.io.*;

class ThrowDemo

{

             public static void main(String args[]) throws Exception

          {

                int number=15;

                 try

               {

                  if(number>10)

                  throw new Exception("Maximum limit is 10");

              }

              catch(Exception e)

             {

                   System.out.println("Exception has been Occured"+e.toString());

            }

            finally

          {

              System.out.println("This is the last statement");

           }

       }

}

 

Answer the Following Questions

                                                                (10 marks)

1.Write a program for finally in Exception handling

File name: FinallyDemo.java

class FinallyDemo{

static void procA(){

try{

System.out.println("inside procA");

throw new RuntimeException("demo");

}finally{

System.out.println("procA's finally");

}

}

static void procB(){

try{

System.out.println("inside procB");

return;

}finally{

System.out.println("procB's finally");

}

}

static void procC(){

try{

System.out.println("inside procC");

}finally{

System.out.println("procC's finally");

}

}

public static void main(String args[]){

try{

procA();

}catch(Exception e){

System.out.println("Exception caught");

}

procB();

procC();

}

}

No comments:

Post a Comment