Skip to content

Commit e23e412

Browse files
committed
feat: Demonstrate multiple try–catch blocks for safe division handling
WHAT the code does: - Defines ExceptionHandling with main(): - Uses Scanner to take two integers from user (d, f). - Declares variables a = 10, b = 0. - Performs two divisions with separate exception handling: 1. a / b: - Always triggers ArithmeticException (division by zero). - Caught and prints error message. 2. d / f: - Depends on user input. - If f = 0 → ArithmeticException caught. - Else prints result. - Prints "Bye" to confirm graceful program termination. - Closes Scanner. WHY this matters: - Demonstrates handling multiple potential exceptions **independently**. - Ensures that failure in one calculation doesn’t prevent the program from continuing to the next. - Reinforces exception handling as a tool to keep applications robust and user-friendly. HOW it works: 1. First try–catch: - Attempts 10 / 0. - ArithmeticException caught → prints "Error: Cannot divide a by b…". 2. Second try–catch: - Attempts d / f with user-provided values. - If denominator f = 0 → prints "Error: Denominator should not be zero…". - Else prints correct result. 3. Program continues execution to "Bye". Tips and gotchas: - Each risky operation should have its own try–catch for precise handling. - Multiple exceptions can be caught in one block using multi-catch (e.g., `catch (ArithmeticException | InputMismatchException e)`). - Always close Scanner (done here) to avoid resource leaks. - Avoid using exceptions for routine control flow—validate input before dividing when possible. Use-cases / analogies: - Like two test flights: if one plane fails (division by zero), the other test (user input division) can still proceed independently. - In calculators or banking systems: prevents system crash when invalid division occurs, while allowing other operations to continue. Short key: java exception-handling multiple-try-catch arithmetic divide-by-zero input-validation. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 36726f8 commit e23e412

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Section18ExceptionHandling/src/ExceptionHandling.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@
33
public class ExceptionHandling {
44
public static void main(String[] args) {
55
Scanner sc = new Scanner(System.in);
6+
67
System.out.println("Enter Two Numbers: ");
8+
79
int d = sc.nextInt();
810
int f = sc.nextInt();
9-
int g = 0; // Initialize to avoid compilation errors
11+
int g = 0; // Initialize to avoid compilation errors.
1012

1113
int a = 10;
1214
int b = 0;
13-
int c = 0; // Initialize to avoid errors
15+
int c = 0; // Initialize to avoid errors.
1416

1517
// Handle division of a / b separately
16-
try
17-
{
18+
try {
1819
c = a / b;
1920
System.out.println("Result of a / b: " + c);
2021
}
21-
catch (ArithmeticException e)
22-
{
22+
23+
catch (ArithmeticException e) {
2324
System.out.println("Error: Cannot divide a by b. " + e);
2425
}
2526

2627
// Handle user input division separately
27-
try
28-
{
28+
try {
2929
g = d / f;
3030
System.out.println("Result of d / f: " + g);
3131
}
32-
catch (ArithmeticException e)
33-
{
32+
33+
catch (ArithmeticException e) {
3434
System.out.println("Error: Denominator should not be zero. " + e);
3535
}
36+
3637
System.out.println("Bye");
3738
sc.close();
3839
}

0 commit comments

Comments
 (0)