Exceptions help programs handle errors. Checked and unchecked exceptions tell us when and how to handle these errors.
Checked vs unchecked exceptions in Java
try { // code that might throw exceptions } catch (ExceptionType e) { // handle exception }
Checked exceptions must be declared or caught.
Unchecked exceptions do not need to be declared or caught.
public void readFile() throws IOException { // code that might throw IOException }
int divide(int a, int b) { return a / b; // might throw ArithmeticException }
This program shows a checked exception (IOException) that must be caught or declared, and an unchecked exception (ArithmeticException) that can be caught but does not have to be declared.
import java.io.*; public class ExceptionDemo { public static void main(String[] args) { try { readFile(); } catch (IOException e) { System.out.println("Caught checked exception: " + e.getMessage()); } try { int result = divide(10, 0); System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("Caught unchecked exception: " + e.getMessage()); } } public static void readFile() throws IOException { throw new IOException("File not found"); } public static int divide(int a, int b) { return a / b; // might throw ArithmeticException } }
Checked exceptions are checked by the compiler at compile time.
Unchecked exceptions are subclasses of RuntimeException and are checked at runtime.
Use checked exceptions for recoverable conditions, unchecked for programming errors.
Checked exceptions must be declared or handled.
Unchecked exceptions do not require declaration or handling.
Checked exceptions are for recoverable errors; unchecked are for bugs.
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
Ready to prove it?
Pass the test and this pattern moves unknown
