Home / Questions / What would be printed by the following code segment if someValue equals 1000?
Explanatory Question

What would be printed by the following code segment if someValue equals 1000?

👁 41 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation


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