What if your program could warn you about problems before they crash everything?
Checked vs unchecked exceptions in Java - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you write a program that reads a file. Without any system to warn you, you might forget to check if the file exists or if you have permission to open it. Your program crashes unexpectedly, and you have no idea why.
Manually checking every possible error before it happens is slow and easy to forget. This leads to bugs that are hard to find and fix. Without clear rules, your code becomes messy and unreliable.
Checked and unchecked exceptions help by making some errors visible and forcing you to handle them. Checked exceptions require you to plan for problems like missing files, while unchecked exceptions handle unexpected bugs. This keeps your code clean and safer.
void readFile() {
// no error checks
FileInputStream file = new FileInputStream("data.txt");
// read file
}void readFile() throws IOException {
FileInputStream file = new FileInputStream("data.txt");
// read file
}This concept lets you write programs that catch problems early and handle them gracefully, making your software more reliable and easier to maintain.
When you download an app, it checks if your internet is connected (checked exception). But if the app crashes due to a bug, that's an unchecked exception you didn't expect.
Checked exceptions force you to handle known problems.
Unchecked exceptions represent unexpected bugs.
Using both helps create safer and clearer code.
Practice
checked exceptions in Java?Solution
Step 1: Understand checked exceptions
Checked exceptions are exceptions that the compiler forces you to handle or declare.Step 2: Compare options with definition
They must be either caught or declared in the method signature. states they must be caught or declared, which matches the definition.Final Answer:
They must be either caught or declared in the method signature. -> Option CQuick Check:
Checked exceptions require handling = A [OK]
- Confusing checked with unchecked exceptions
- Thinking checked exceptions are always bugs
- Believing checked exceptions don't need handling
IOException?Solution
Step 1: Recall correct syntax for throws
The correct syntax uses the keywordthrowsfollowed by the exception class name without parentheses.Step 2: Analyze each option
public void readFile() throws IOException {} matches the correct syntax exactly. Options A, B, and C have syntax errors.Final Answer:
public void readFile() throws IOException {} -> Option AQuick Check:
Correct throws syntax = D [OK]
- Using 'throw' instead of 'throws' in method signature
- Adding parentheses after exception name
- Trying to instantiate exception in throws clause
public class Test {
public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught unchecked exception");
}
}
}Solution
Step 1: Identify exception type
Dividing by zero causes anArithmeticException, which is an unchecked exception.Step 2: Check catch block handling
The catch block catchesArithmeticExceptionand prints "Caught unchecked exception".Final Answer:
Caught unchecked exception -> Option DQuick Check:
Unchecked exceptions can be caught = B [OK]
- Thinking unchecked exceptions cause compile errors
- Assuming program crashes without output
- Confusing checked and unchecked exceptions
public void readFile() {
FileReader fr = new FileReader("file.txt");
}What is the error and how to fix it?
Solution
Step 1: Identify exception type from FileReader
TheFileReaderconstructor throws a checkedIOException.Step 2: Fix by handling or declaring exception
We must either surround with try-catch or declarethrows IOExceptionin method signature.Final Answer:
FileReader constructor throws checked IOException; add try-catch or declare throws. -> Option BQuick Check:
Checked exceptions require handling = C [OK]
- Ignoring checked exception requirement
- Confusing package for FileReader
- Thinking FileReader throws unchecked exceptions
IOException internally but let runtime exceptions propagate. Which approach correctly applies checked vs unchecked exceptions?Solution
Step 1: Understand handling checked exceptions
Checked exceptions like IOException should be handled or declared. Here, handling internally means try-catch inside method.Step 2: Understand unchecked exceptions handling
Unchecked exceptions (RuntimeExceptions) usually indicate bugs and should propagate to caller, so do not catch them here.Final Answer:
Use try-catch for IOException inside method; do not catch RuntimeExceptions. -> Option AQuick Check:
Handle checked, propagate unchecked = A [OK]
- Catching unchecked exceptions unnecessarily
- Declaring checked exceptions when handling internally
- Ignoring exception handling rules
