Simple Exception Handling Example

This program demonstrates basic try-catch exception handling.

Problem Statement

Write a Java program that divides two numbers and handles division by zero using try-catch.

Source Code

1 public class BasicTryCatch {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Basic Try-Catch Example");
4 try {
5 int a=10, b=0;
6 int c=a/b;
7 System.out.println("Eduinq Result: "+c);
8 } catch(ArithmeticException e) {
9 System.out.println("Eduinq Exception: Division by zero not allowed");
10 }
11 }
12 }

Program Output

					Eduinq Basic Try-Catch Example
Eduinq Exception: Division by zero not allowed
				

Explanation

The try block contains risky code that may throw an exception. - Division by zero triggers ArithmeticException. - The catch block handles the exception gracefully, preventing program crash. - Exception handling improves reliability and user experience. - In enterprise systems, try-catch ensures stable execution even under unexpected conditions. Conclusion: Shows basic try-catch exception handling.

Quick Links to Explore