✏️ Explanatory Question

What are the three ways that an exception can be generated?

👁 34 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

An exception can be generated in three ways:

  1. By the JVM (Java Virtual Machine) – for example, division by zero or accessing an invalid array index.

  2. By the program code – using the throw keyword explicitly.

  3. By calling a method that declares an exception in its throws clause.

Example:


throw new ArithmeticException("Division by zero");