Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a checked exception in Java?
A checked exception is an exception that the compiler forces you to handle. You must either catch it with a try-catch block or declare it with a throws clause in the method signature.
Click to reveal answer
beginner
What is an unchecked exception in Java?
An unchecked exception is an exception that the compiler does not force you to handle. These usually come from programming errors like null pointer access or array index out of bounds.
Click to reveal answer
beginner
Give an example of a checked exception.
Examples of checked exceptions include IOException, SQLException, and ClassNotFoundException. These often relate to external resources or conditions outside the program's control.
Click to reveal answer
beginner
Give an example of an unchecked exception.
Examples of unchecked exceptions include NullPointerException, ArrayIndexOutOfBoundsException, and IllegalArgumentException. These usually indicate bugs in the code.
Click to reveal answer
intermediate
Why does Java distinguish between checked and unchecked exceptions?
Java distinguishes them to encourage programmers to handle expected problems (checked exceptions) while not forcing handling of programming errors (unchecked exceptions), making code safer and clearer.
Click to reveal answer
Which of the following is a checked exception?
AIOException
BNullPointerException
CArrayIndexOutOfBoundsException
DIllegalArgumentException
✗ Incorrect
IOException is a checked exception that must be handled or declared.
Unchecked exceptions are usually caused by:
AProgramming errors
BFile not found
CNetwork issues
DExternal resource failures
✗ Incorrect
Unchecked exceptions often come from bugs like null pointer access or invalid arguments.
What must you do when a method throws a checked exception?
AIgnore it
BCatch it or declare it with throws
COnly catch it
DOnly declare it with throws
✗ Incorrect
You must either catch the checked exception or declare it in the method signature.
Which class is the root of unchecked exceptions?
AException
BThrowable
CError
DRuntimeException
✗ Incorrect
RuntimeException is the base class for unchecked exceptions.
If you forget to handle a checked exception, what happens?
ARuntime error
BProgram compiles fine
CCompiler error
DWarning only
✗ Incorrect
The compiler will give an error if a checked exception is not handled or declared.
Explain the difference between checked and unchecked exceptions in Java.
Think about compiler enforcement and typical causes.
You got /4 concepts.
Describe a real-life situation where you might encounter a checked exception and how you would handle it.
Imagine opening a file that might not exist.
You got /3 concepts.
Practice
(1/5)
1. Which of the following statements correctly describes checked exceptions in Java?
easy
A. They are runtime exceptions that do not require handling.
B. They are errors that cannot be handled by the program.
C. They must be either caught or declared in the method signature.
D. They are always caused by bugs in the code.
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 C
Quick Check:
Checked exceptions require handling = A [OK]
Hint: Checked exceptions need try-catch or throws declaration [OK]
Common Mistakes:
Confusing checked with unchecked exceptions
Thinking checked exceptions are always bugs
Believing checked exceptions don't need handling
2. Which of the following is the correct way to declare a method that throws a checked exception IOException?
easy
A. public void readFile() throws IOException {}
B. public void readFile() throw IOException {}
C. public void readFile() throws IOException() {}
D. public void readFile() throws new IOException {}
Solution
Step 1: Recall correct syntax for throws
The correct syntax uses the keyword throws followed 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 A
Quick Check:
Correct throws syntax = D [OK]
Hint: Use 'throws ExceptionName' without parentheses [OK]
Common Mistakes:
Using 'throw' instead of 'throws' in method signature
Adding parentheses after exception name
Trying to instantiate exception in throws clause
3. What will be the output of the following Java code?
public class Test {
public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught unchecked exception");
}
}
}
medium
A. Compilation error due to unchecked exception
B. Caught checked exception
C. No output, program crashes silently
D. Caught unchecked exception
Solution
Step 1: Identify exception type
Dividing by zero causes an ArithmeticException, which is an unchecked exception.
Step 2: Check catch block handling
The catch block catches ArithmeticException and prints "Caught unchecked exception".
Final Answer:
Caught unchecked exception -> Option D
Quick Check:
Unchecked exceptions can be caught = B [OK]
Hint: ArithmeticException is unchecked and can be caught [OK]
Common Mistakes:
Thinking unchecked exceptions cause compile errors
Assuming program crashes without output
Confusing checked and unchecked exceptions
4. Consider this code snippet:
public void readFile() {
FileReader fr = new FileReader("file.txt");
}
What is the error and how to fix it?
medium
A. FileReader constructor syntax is wrong; remove parentheses.
B. FileReader constructor throws checked IOException; add try-catch or declare throws.
C. FileReader is an unchecked exception; no fix needed.
D. FileReader must be imported from java.util package.
Solution
Step 1: Identify exception type from FileReader
The FileReader constructor throws a checked IOException.
Step 2: Fix by handling or declaring exception
We must either surround with try-catch or declare throws IOException in method signature.
Final Answer:
FileReader constructor throws checked IOException; add try-catch or declare throws. -> Option B
Quick Check:
Checked exceptions require handling = C [OK]
Hint: Checked exceptions must be caught or declared [OK]
Common Mistakes:
Ignoring checked exception requirement
Confusing package for FileReader
Thinking FileReader throws unchecked exceptions
5. You want to write a method that reads a file and returns its first line. The method should handle IOException internally but let runtime exceptions propagate. Which approach correctly applies checked vs unchecked exceptions?
hard
A. Use try-catch for IOException inside method; do not catch RuntimeExceptions.
B. Declare method throws IOException; catch RuntimeExceptions inside method.
C. Catch both IOException and RuntimeException inside method.
D. Do not catch any exceptions; let all propagate.
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 A
Quick Check:
Handle checked, propagate unchecked = A [OK]
Hint: Catch checked exceptions; let unchecked exceptions propagate [OK]
Common Mistakes:
Catching unchecked exceptions unnecessarily
Declaring checked exceptions when handling internally