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); } } } |
chevron_right
filter_none
(A) Got the Exception 10
(B) Got the Exception 0
(C) Compiler Error
Answer: (C)
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
Recommended Posts:
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 4
- Java | Exception Handling | Question 6
- Java | Exception Handling | Question 7
- Java | Exception Handling | Question 3
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 2
- Nested try blocks in Exception Handling in Java
- Output of Java program | Set 12(Exception Handling)
- Exception Handling with Method Overriding in Java
- C++ | Exception Handling | Question 12
- C++ | Exception Handling | Question 8
- C++ | Exception Handling | Question 7
- C++ | Exception Handling | Question 5
- C++ | Exception Handling | Question 2