Home / Questions / What is an Exception in Java?
Explanatory Question

What is an Exception in Java?

👁 30 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 in Java is an unexpected event or error that happens while a program is running, which interrupts the normal flow of the program.

It’s Java’s way of saying:

“Something went wrong, but I can handle it gracefully without crashing the whole program.”

🧠 Example:


int a = 10;
int b = 0;
int result = a / b;   // ❌ This will cause an exception
System.out.println(result);

👉 Here, a / b will cause an ArithmeticException because you can’t divide by zero.

Without handling this, your program will crash.