DEV Community

Cover image for Java Exception Handling – Practical Scenarios Explained
KIRUBAGARAN .K
KIRUBAGARAN .K

Posted on

Java Exception Handling – Practical Scenarios Explained

Exception handling in Java is not just about catching errors, but about designing programs that behave predictably even when things go wrong. Let’s explore three important real-world scenarios to understand how exceptions should be used effectively.

1.Student Result System

Problem Statement

You are building a system to store student marks. The valid range is between 0 and 100.

Conditions

If marks < 0 → throw exception
If marks > 100 → throw exception

public class StudentResult {
    public static void main(String[] args) {
        int marks = 110; 

        if (marks < 0 || marks > 100) {
            throw new IllegalArgumentException("Invalid marks! Marks should be between 0 and 100 ");
        }

        System.out.println("Valid marks: " + marks);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • This uses an unchecked exception (IllegalArgumentException)
  • It is appropriate because invalid marks indicate a programming or input validation issue
  • The program immediately stops when invalid data is detected, preventing incorrect results

2.Checked vs Unchecked Exceptions – When to Use?

Checked

Checked exceptions are used when the error is expected and recoverable.

Java forces you to handle them using try-catch or throws. For example, opening a file may throw an IOException, and the program can recover by asking for another file.

FileReader file = new FileReader("test.txt"); // IOException

When to Use

  • The error is expected
  • The error is recoverable
  • The caller should be forced to handle it

Unchecked

Unchecked exceptions are used for programming mistakes that are not recoverable at runtime.

These occur due to logical errors in code, like dividing by zero, which throws an ArithmeticException. Instead of handling them, the correct approach is to fix the code.

int a = 10 / 0; // ArithmeticException

When to Use

  • The error is due to a programming mistake
  • It is not recoverable at runtime
  • No need to force handling

3.Will finally Execute if System.exit(0)is Called? (To be discussed)

When System.exit(0) runs, it immediately shuts down the JVM, like pulling the plug on the entire program. Because of this sudden stop, the normal flow of execution is interrupted, and the finally block is skipped completely.

public class TestFinally {
    public static void main(String[] args) {
        try {
            System.out.println("Try block");
            System.exit(0);
        } finally {
            System.out.println("Finally block");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

No, the finally block will not execute if System.exit(0) is called.

What System.exit(0) Does

  • Immediately terminates the Java Virtual Machine (JVM)
  • Stops all running threads
  • Ends program execution abruptly

Why Finally Does Not Execute

  • The finally block is part of normal program flow
  • System.exit(0) bypasses this flow completely
  • The JVM shuts down before finally gets a chance to run

Top comments (0)