Divided by zero with exception handling
Steps to Handle Division by Zero in Java
1. Use try
block
-
Put the code that might throw an exception (like division) inside the
try
block.
2. Use catch
block
-
Catch the specific exception:
ArithmeticException
for divide by zero.
3. Optional: finally
block
-
This runs no matter what—used to clean up (optional).
🔢 Example Code
💡 Output
✅ Summary
Step | What You Do |
---|---|
1 | Use try to write risky code |
2 | Use catch (ArithmeticException e) |
3 | Optionally use finally |