What would be printed by the following code segment if someValue equals 1000?
Answer:
int M = someValue; try { System.out.println("Entering try block"); if (M > 100) throw new Exception(M + " is too large"); System.out.println("Exiting try block"); } catch (Exception e) { System.out.println("ERROR: " + e.getMessage()); }
Solution:
If someValue equals 1000, the condition (M > 100) becomes true, so an exception is thrown.
The control jumps directly to the catch block, skipping the line "Exiting try block".
Output:
Entering try block ERROR: 1000 is too large
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.