Common mistakes and practice
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Most exception bugs come from handling exceptions badly, not from the exceptions themselves. An empty catch block or a catch-everything block can hide a real problem.
Empty catch blocks hide problems
A fee list has 15OO (with the letter O), and the total comes out ₹1500 short with no warning. A catch block with nothing inside is called swallowing the exception: the program goes on, but nobody knows something went wrong.
Example 1 · An empty catch hides bad data
public class EmptyCatch { public static void main(String[] args) { String[] fees = {"1200", "15OO", "800"}; int total = 0; for (String f : fees) { try { total += Integer.parseInt(f); } catch (NumberFormatException e) { // nothing here } } System.out.println("Total fees: " + total); }}Total fees: 2000
What this code does
"15OO"has the letter O, not zero, soparseIntthrows.- The empty catch swallows it: ₹1500 goes missing and the output gives no hint.
Example 2 · The fix: say what was skipped
public class FeeTotal { public static void main(String[] args) { String[] fees = {"1200", "15OO", "800"}; int total = 0; for (String f : fees) { try { total += Integer.parseInt(f); } catch (NumberFormatException e) { System.out.println("Bad fee skipped: " + f); } } System.out.println("Total fees: " + total); }}Bad fee skipped: 15OO Total fees: 2000
What this code does
- The total is the same, but now the program says which value was wrong, so someone can fix it.
- The catch is for
NumberFormatExceptiononly, so a different bug would still show up.
Always do something in catch: print a clear message, use a safe value, or ask again. A quietly wrong total is worse than a crash, because nobody goes looking for it.
One catch-all hides a real bug
catch (Exception e) around a whole program gives every problem the same reply, so a bug in your own loop looks like bad user input. Catch the specific type you expect, around the smallest risky part.
Example 3 · Blaming the user for a loop bug
public class CatchAll { public static void main(String[] args) { String[] marks = {"78", "85", "62"}; int total = 0; try { for (int i = 0; i <= marks.length; i++) { total += Integer.parseInt(marks[i]); } System.out.println("Total: " + total); } catch (Exception e) { System.out.println("Please check your input"); } }}Please check your input
What this code does
- The input is fine. The real bug is
i <= marks.length, which reads one element past the end. - The catch-all blames the user, and the total is never printed.
With only catch (NumberFormatException e) inside the loop, the real bug would show at once as ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3. Review: Multiple catch blocks (opens in a new tab).
Spot the bug
The other common slips each look almost right. Read what each program should do, tap the line that is wrong, then check the fix.
Two more to remember:
- A value you can check first (a count of 0, an index past the end) → use
if, not catch. Review: Common exceptions (opens in a new tab) - Clean-up written after the try-catch → skipped when nothing catches the exception; put it in
finally. Review: The finally block (opens in a new tab)
Find the bug
Spot the bug
Select the line that is wrong.
This should stop a negative age, but it does not compile.
Round 1 of 4When in doubt, trace it
A loop with a try, two catch blocks and a finally is hard to follow just by reading. Step through it and watch which block runs for each input.
Trace the code
Trace try, catch and finally in a loop
Variables
- s
- 4
Output
(nothing printed yet)Line 4First round: s is "4".
Finish up
Mark complete & continue
Next: Must-do problems: Exception Basics