Java Exception Handling
Question 1 |
Predict the output of following Java program
class Main { public static void main(String args[]) { try { throw 10; } catch(int e) { System.out.println("Got the Exception " + e); } } }
Got the Exception 10 | |
Got the Exception 0 | |
Compiler Error |
Discuss it
Question 1 Explanation:
In Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. So basic data type can no be thrown at all.
Following are errors in the above program
Main.java:4: error: incompatible types throw 10; ^ required: Throwable found: int Main.java:6: error: unexpected type catch(int e) { ^ required: class found: int 2 errors
Question 2 |
class Test extends Exception { } class Main { public static void main(String args[]) { try { throw new Test(); } catch(Test t) { System.out.println("Got the Test Exception"); } finally { System.out.println("Inside finally block "); } } }
Got the Test Exception Inside finally block | |
Got the Test Exception | |
Inside finally block | |
Compiler Error |
Discuss it
Question 2 Explanation:
In Java, the finally is always executed after the try-catch block. This block can be used to do the common cleanup work. There is no such block in C++.
Question 3 |
Output of following Java program?
class Main { public static void main(String args[]) { int x = 0; int y = 10; int z = y/x; } }
Compiler Error | |
Compiles and runs fine | |
Compiles fine but throws ArithmeticException exception |
Discuss it
Question 3 Explanation:
ArithmeticException is an unchecked exception, i.e., not checked by the compiler. So the program compiles fine. See following for more details.
Checked vs Unchecked Exceptions in Java
Question 4 |
class Base extends Exception {} class Derived extends Base {} public class Main { public static void main(String args[]) { // some other stuff try { // Some monitored code throw new Derived(); } catch(Base b) { System.out.println("Caught base class exception"); } catch(Derived d) { System.out.println("Caught derived class exception"); } } }
Caught base class exception | |
Caught derived class exception | |
Compiler Error because derived is not throwable | |
Compiler Error because base class exception is caught before derived class |
Discuss it
Question 4 Explanation:
See Catching base and derived classes as exceptions
Following is the error in below program
Main.java:12: error: exception Derived has already been caught catch(Derived d) { System.out.println("Caught derived class exception"); }
Question 5 |
class Test { public static void main (String[] args) { try { int a = 0; System.out.println ("a = " + a); int b = 20 / a; System.out.println ("b = " + b); } catch(ArithmeticException e) { System.out.println ("Divide by zero error"); } finally { System.out.println ("inside the finally block"); } } }
Compile error | |
Divide by zero error | |
a = 0 Divide by zero error inside the finally block | |
a = 0 | |
inside the finally block |
Discuss it
Question 5 Explanation:
On division of 20 by 0, divide by zero exception occurs and control goes inside the catch block.
Also, the finally block is always executed whether an exception occurs or not.
Question 6 |
class Test { public static void main(String[] args) { try { int a[]= {1, 2, 3, 4}; for (int i = 1; i <= 4; i++) { System.out.println ("a[" + i + "]=" + a[i] + "n"); } } catch (Exception e) { System.out.println ("error = " + e); } catch (ArrayIndexOutOfBoundsException e) { System.out.println ("ArrayIndexOutOfBoundsException"); } } }
Compiler error | |
Run time error | |
ArrayIndexOutOfBoundsException | |
Error Code is printed | |
Array is printed |
Discuss it
Question 6 Explanation:
ArrayIndexOutOfBoundsException has been already caught by base class Exception. When a subclass exception is mentioned after base class exception, then error occurs.
Question 7 |
Predict the output of the following program.
class Test { String str = "a"; void A() { try { str +="b"; B(); } catch (Exception e) { str += "c"; } } void B() throws Exception { try { str += "d"; C(); } catch(Exception e) { throw new Exception(); } finally { str += "e"; } str += "f"; } void C() throws Exception { throw new Exception(); } void display() { System.out.println(str); } public static void main(String[] args) { Test object = new Test(); object.A(); object.display(); } }
abdef | |
abdec | |
abdefc |
Discuss it
Question 7 Explanation:
'throw' keyword is used to explicitly throw an exception.
finally block is always executed even when an exception occurs.
Call to method C() throws an exception. Thus, control goes in catch block of method B() which again throws an exception. So, control goes in catch block of method A().
Question 8 |
Predict the output of the following program.
class Test { int count = 0; void A() throws Exception { try { count++; try { count++; try { count++; throw new Exception(); } catch(Exception ex) { count++; throw new Exception(); } } catch(Exception ex) { count++; } } catch(Exception ex) { count++; } } void display() { System.out.println(count); } public static void main(String[] args) throws Exception { Test obj = new Test(); obj.A(); obj.display(); } }
4 | |
5 | |
6 | |
Compilation error |
Discuss it
Question 8 Explanation:
‘throw’ keyword is used to explicitly throw an exception.
In third try block, exception is thrown. So, control goes in catch block.
Again, in catch block exception is thrown. So, control goes in inner catch block.
Question 9 |
Which of these is a super class of all errors and exceptions in the Java language?
RunTimeExceptions | |
Throwable | |
Catchable | |
None of the above |
Discuss it
Question 9 Explanation:
All the errors and exception types are subclasses of the built in class Throwable in the java language. Throwable class is the superclass of all errors and exceptions in the Java language.
Option (B) is correct.
Question 10 |
The built-in base class in Java, which is used to handle all exceptions is
Raise | |
Exception | |
Error | |
Throwable |
Discuss it
Question 10 Explanation:
Throwable class is the built-in base class used to handle all the exceptions in Java.
There are 10 questions to complete.