Home / Questions / What is wrong with this fragment?
Explanatory Question

What is wrong with this fragment?

👁 33 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


// • • •
vals[18] = 10;
catch (ArrayIndexOutOfBoundsException exc) {
    // handle error
}

Solution:

The error is that there is no try block before the catch statement.
A catch block must follow a try block, otherwise the compiler will throw an error.
The corrected code should be: