Home / Questions / What are the three ways that an exception can be generated?
Explanatory Question

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

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

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");