Output of Java Programs | Set 41 (try-catch)
Prerequisite : try-catch, Exception Handling
1. What will be the output of the following program?
class Geeks { public static void main(String[] args) { try { System.out.println(1/0); } catch(ArithmeticException e) { System.out.println(e.getMessage()); } } } |
Options:
1. java.lang.ArithmeticExcetion
2. / by zero
3. java.lang.ArithmeticExcetion:/ by zero
4. ArithmeticExcetion
The answer is option (2)
Explanation: In the above program, we are calling getMessage() method to print the exception information. We know that getMessage() method will always be printed as the description of the exception which is / by zero.
2. What will be the output of the following program?
class Geeks { public static void main(String[] args) { try { System.out.println(1/0); } catch(ArithmeticException e) { System.out.println("Hello Geeks"); } catch(Exception e) { System.out.println("Welcome"); } } } |
Options:
1. Hello Geeks
2. No Output
3. Compile-time error
4. welcome
The answer is option (1)
Explanation: In the above program, we are following the approach of try with multiple catch blocks. Here 1/0 is an ArithmeticException, which is cought by the first catch block and it is executed.
3. What will be the output of the following program?
class Geeks { public static void main(String[] args) { try { System.out.println(1/0); } catch(Exception e) { System.out.println("Hello Geeks"); } catch(ArithmeticException e) { System.out.println("Welcome"); } } } |
Options:
1. Hello Geeks
2. No Output
3. Compile-time error
4. welcome
The answer is option (3)
Explanation: If we are trying try with multiple catch block then we should take care that the child class catch block is first then parent class catch block. Otherwise we will get compile time error saying error: exception ArithmeticException has already been caught.
4. What will be the output of the following program?
class Geeks { public static void main(String[] args) { try { System.out.println(1/0); } } } |
Options:
1. Run-time Exception
2. Compile-time error
3. No Output
4. Compile-time Exception
The answer is option (2)
Explanation: In the above program, we are declaring a try block without any catch or finally block. We have to always declare try with catch or finally block because single try block is invalid. That’s Why it will give compile time error saying error: ‘try’ without ‘catch’, ‘finally’ or resource declarations.
5. What will be the output of the following program?
class Geeks { public static void main(String[] args) { try { System.out.println(1/0); } System.out.println("Hello GEEKS"); catch(ArithmeticException e) { System.out.println("Welcome"); } } } |
Options:
1. Hello Geeks
2. Hello Geeks
Welome
3. Run-time Exception
4. Compile-time error
The answer is option (4)
Explanation: In the above program, we are declaring a try block and also a catch block but both are separated by a single line which will cause compile time error:
prog.java:5: error: 'try' without 'catch', 'finally' or resource declarations
try
^
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Output of Java Programs | Set 50
- Output of Java Programs | Set 12
- Output of Java Programs | Set 37 (If-else)
- Output of Java Programs | Set 31
- Output of Java Programs | Set 32
- Output of Java programs | Set 29
- Output of Java Programs | Set 30
- Output of Java Programs | Set 49
- Output of Java Programs | Set 40 (for loop)
- Output of Java Programs | Set 34 (Collections)
- Output of Java programs | Set 13 (Collections)
- Output of Java Programs | Set 14 (Constructors)
- Output of Java Programs | Set 42 (Arrays)
- Output of Java Programs | Set 33 (Collections)
- Output of Java Programs | Set 47 (Arrays)



