Skip to content

Commit 094dad2

Browse files
committed
feat: Add TryCatch demo handling division by zero with user input
WHAT the code does: - Defines TryCatch with main() to demonstrate structured exception handling. - Uses Scanner to read two integers d and f from user. - Declares variables: - a = 10, b = 0 → deliberate zero denominator. - c and g initialized to 0 for safe compilation. - First try–catch block: - Attempts a / b → triggers ArithmeticException. - Caught and prints "Error: Cannot divide a by b". - Second try–catch block: - Attempts d / f (user-provided values). - If f == 0, triggers ArithmeticException. - Caught and prints "Error: Denominator should not be zero". - Prints "Bye" at the end, showing program continues gracefully. WHY this matters: - Demonstrates how try–catch blocks allow safe handling of runtime exceptions. - Shows that different risky operations can be isolated in separate try–catch blocks. - Highlights the difference between a pre-known division by zero (a / b) and a user-input scenario (d / f). - Reinforces best practice of validating inputs and handling exceptions instead of letting program crash. HOW it works: 1. Program starts, prompts user for two numbers. 2. Division a / b runs inside try: - Since b = 0, throws ArithmeticException. - catch block prints error message. 3. Division d / f runs with user input: - If f ≠ 0, prints result. - If f = 0, ArithmeticException caught, error printed. 4. Execution continues and "Bye" prints regardless of errors. Tips and gotchas: - Closing Scanner is good practice (done here). - Avoid dividing without validating denominator; input checks are preferable to relying only on try–catch. - Keep try–catch blocks as small as possible (only wrap risky code). - Generic Exception should not be used unless necessary; catching specific exceptions is cleaner and safer. Use-cases / analogies: - Calculator applications: division operations need safe handling of zero denominators. - Banking systems: avoid division errors in interest calculations or currency conversions. - User-driven apps: exception handling ensures invalid input does not crash the system. Short key: java exception-handling try-catch division-by-zero input-validation. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent d7e6f2c commit 094dad2

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Section18ExceptionHandling/src/TryCatch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ public class TryCatch {
44
public static void main(String[] args) {
55
Scanner sc = new Scanner(System.in);
66
System.out.println("Enter Two Numbers: ");
7+
78
int d = sc.nextInt();
89
int f = sc.nextInt();
9-
int g = 0; // Initialize to avoid compilation errors
10+
int g = 0; // Initialize to avoid compilation errors.
1011

1112
int a = 10;
1213
int b = 0;
13-
int c = 0; // Initialize to avoid errors
14+
int c = 0; // Initialize to avoid errors.
1415

1516
// Handle division of a / b separately
1617
try {
@@ -20,14 +21,13 @@ public static void main(String[] args) {
2021
System.out.println("Error: Cannot divide a by b. " + e);
2122
}
2223

23-
// Handle user input division separately
24+
// Handle user input division separately.
2425
try {
2526
g = d / f;
2627
System.out.println("Result of d / f: " + g);
2728
} catch (ArithmeticException e) {
2829
System.out.println("Error: Denominator should not be zero. " + e);
2930
}
30-
3131
System.out.println("Bye");
3232
sc.close();
3333
}

0 commit comments

Comments
 (0)