✏️ Explanatory Question

How do you handle an exception? Describe the keyword try. How is it used?

👁 66 Views
📘 Detailed Answer
🔴 Hard
💡

Answer with Explanation

We handle exceptions using a try-catch block.
The try block contains code that may throw an exception.
The catch block handles it.

Example:


try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: " + e.getMessage());
}