logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Exceptions See What Others Are Saying!
  • Question
  • What will be the output of the program?
    try 
    { 
        int x = 0; 
        int y = 5 / x; 
    } 
    catch (Exception e) 
    {
        System.out.println("Exception"); 
    } 
    catch (ArithmeticException ae) 
    {
        System.out.println(" Arithmetic Exception"); 
    } 
    System.out.println("finished");
    


  • Options
  • A. finished
  • B. Exception
  • C. Compilation fails.
  • D. Arithmetic Exception

  • Correct Answer
  • Compilation fails. 

    Explanation
    Compilation fails because ArithmeticException has already been caught. ArithmeticException is a subclass of java.lang.Exception, by time the ArithmeticException has been specified it has already been caught by the Exception class.

    If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses).


    More questions

    • 1. What will be the output of the program (in jdk1.6 or above)?
      public class BoolTest 
      {
          public static void main(String [] args) 
          {
              Boolean b1 = new Boolean("false");
              boolean b2;
              b2 = b1.booleanValue();
              if (!b2) 
              {
                  b2 = true;
                  System.out.print("x ");
              }
              if (b1 & b2) /* Line 13 */
              {
                  System.out.print("y ");
              }
              System.out.println("z");
          }
      }
      

    • Options
    • A. z
    • B. x z