EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

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

java
public class DivideByZeroExample {
public static void main(String[] args) {
try {
int a = 10;
int b = 0; // Division by zero
int result = a / b; // This will throw ArithmeticException
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
} finally {
System.out.println("Division attempt completed.");
}
}
}

💡 Output

vbnet
Error: Cannot divide by zero.
Division attempt completed.

✅ Summary

Step What You Do
1 Use try to write risky code
2 Use catch (ArithmeticException e)
3 Optionally use finally

Othersubjects

Divided by zero with exception handling

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer