Fundamentals 9 min read

Top 10 Java Exception Pitfalls Every Developer Should Know

This article compiles the ten most common Java exception questions, covering checked vs unchecked differences, best‑practice guidelines, variable scope in try‑catch, inconsistent null parsing errors, frequent runtime exceptions, multi‑catch syntax, constructor throws, finally‑block behavior, and why developers sometimes ignore exceptions.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Top 10 Java Exception Pitfalls Every Developer Should Know

1. Checked vs Unchecked Exceptions

Checked exceptions are verified at compile time, so methods must either catch them or declare them in the signature. Unchecked exceptions occur at runtime (e.g., division by zero, NullPointerException) and cannot be predicted beforehand. For example, IOException is checked, while RuntimeException is unchecked.

2. Best Practices for Exception Management

If you can handle an exception correctly, catch and process it; otherwise, let it propagate.

3. Why Variables Declared in try Cannot Be Used in catch or finally

Code inside the try block may never reach the variable declaration if an exception is thrown earlier, so the compiler disallows referencing such variables in catch or finally.

try {
    File file = new File("path");
    FileInputStream fis = new FileInputStream(file);
    String s = "inside";
} catch (FileNotFoundException e) {
    e.printStackTrace();
    System.out.println(s); // compile‑time error
}

4. Different Exceptions from Double.parseDouble(null) and Integer.parseInt(null)

The two methods throw different exceptions because they were implemented by different JDK teams. Integer.parseInt(null) throws NumberFormatException, while Double.parseDouble(null) throws NullPointerException.

Integer.parseInt(null); // throws java.lang.NumberFormatException: null
Double.parseDouble(null); // throws java.lang.NullPointerException

5. Common Runtime Exceptions in Java

IllegalArgumentException ArrayIndexOutOfBoundsException

These exceptions are often used to signal that a method argument does not meet expectations. Example:

if (obj == null) {
    throw new IllegalArgumentException("obj can not be null");
}

6. Catching Multiple Exceptions in a Single Catch Clause

Since Java 7 you can catch several unrelated exceptions in one clause using the | operator. Before Java 7 you needed separate catch blocks.

// Java 6 style
catch (AException a) { ... }
catch (BException b) { ... }
catch (CException c) { ... }

// Java 7 style
catch (AException | BException | CException ex) {
    logger.error(ex);
    throw new MyException(ex);
}
Note: The | operator works only when the caught exceptions do not share a common superclass other than Exception . If they inherit from the same parent, you must catch the parent type.

7. Can Constructors Throw Exceptions?

Yes. Constructors are ordinary methods and may declare thrown exceptions.

class FileReader {
    public FileInputStream fis = null;
    public FileReader() throws IOException {
        File dir = new File(".");
        File fin = new File(dir.getCanonicalPath() + File.separator + "not-existing-file.txt");
        fis = new FileInputStream(fin);
    }
}

8. Throwing Exceptions in a finally Block

The following code is legal, but for readability it is better to extract the risky code into a separate method and call it from finally.

public static void main(String[] args) {
    File file1 = new File("path1");
    File file2 = new File("path2");
    try {
        FileInputStream fis = new FileInputStream(file1);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            FileInputStream fis = new FileInputStream(file2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

public static void main(String[] args) {
    // ... same setup ...
    try {
        FileInputStream fis = new FileInputStream(file1);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        // encapsulated method
        methodThrowException();
    }
}

9. Does finally Execute When try Returns?

Yes. The Java Language Specification guarantees that the finally block always runs when the try block exits, even if the try contains a return, break, or continue.

10. Why Some Developers Ignore Exceptions

A common anti‑pattern is catching Exception and merely printing the stack trace, which hides errors and makes debugging harder.

try {
    // ...
} catch (Exception e) {
    e.printStackTrace();
}

References

Unchecked exceptions in Java

The root of Java exception class hierarchy

Java exceptions related questions on StackOverflow

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javabest practicesCode ExamplesChecked ExceptionsUnchecked Exceptions
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.