Bird
Raised Fist0
Javaprogramming~10 mins

Checked vs unchecked exceptions in Java - Interactive Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a method that throws a checked exception.

Java
public void readFile() throws [1] {
    // method body
}
Drag options to blanks, or click blank then click option'
ARuntimeException
BNullPointerException
CError
DIOException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using unchecked exceptions like RuntimeException in throws clause.
2fill in blank
medium

Complete the code to catch an unchecked exception.

Java
try {
    int result = 10 / 0;
} catch ([1] e) {
    System.out.println("Cannot divide by zero");
}
Drag options to blanks, or click blank then click option'
AFileNotFoundException
BIOException
CArithmeticException
DSQLException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Catching checked exceptions unrelated to arithmetic operations.
3fill in blank
hard

Fix the error in the code by choosing the correct exception type to declare.

Java
public void connect() throws [1] {
    // code that may throw exception
}
Drag options to blanks, or click blank then click option'
ASQLException
BNullPointerException
CRuntimeException
DIllegalArgumentException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Declaring unchecked exceptions like RuntimeException in throws clause.
4fill in blank
hard

Fill both blanks to complete the code that handles a checked exception properly.

Java
try {
    FileReader file = new FileReader("file.txt");
} catch ([1] e) {
    System.out.println("File not found: " + e.getMessage());
} finally {
    [2]
}
Drag options to blanks, or click blank then click option'
AFileNotFoundException
BIOException
Cfile.close()
DSystem.exit(0)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Catching wrong exception types or forgetting to close the file.
5fill in blank
hard

Fill all three blanks to create a method that throws a checked exception and handles an unchecked exception.

Java
public void process() throws [1] {
    try {
        int[] arr = new int[5];
        int x = arr[10];
    } catch ([2] e) {
        System.out.println("Index error: " + e.getMessage());
    }
    throw new [3]("File error");
}
Drag options to blanks, or click blank then click option'
AIOException
BArrayIndexOutOfBoundsException
CFileNotFoundException
DRuntimeException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Mixing checked and unchecked exceptions incorrectly.

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

  1. Step 1: Understand checked exceptions

    Checked exceptions are exceptions that the compiler forces you to handle or declare.
  2. 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.
  3. Final Answer:

    They must be either caught or declared in the method signature. -> Option C
  4. 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

  1. Step 1: Recall correct syntax for throws

    The correct syntax uses the keyword throws followed by the exception class name without parentheses.
  2. Step 2: Analyze each option

    public void readFile() throws IOException {} matches the correct syntax exactly. Options A, B, and C have syntax errors.
  3. Final Answer:

    public void readFile() throws IOException {} -> Option A
  4. 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

  1. Step 1: Identify exception type

    Dividing by zero causes an ArithmeticException, which is an unchecked exception.
  2. Step 2: Check catch block handling

    The catch block catches ArithmeticException and prints "Caught unchecked exception".
  3. Final Answer:

    Caught unchecked exception -> Option D
  4. 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

  1. Step 1: Identify exception type from FileReader

    The FileReader constructor throws a checked IOException.
  2. Step 2: Fix by handling or declaring exception

    We must either surround with try-catch or declare throws IOException in method signature.
  3. Final Answer:

    FileReader constructor throws checked IOException; add try-catch or declare throws. -> Option B
  4. 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

  1. Step 1: Understand handling checked exceptions

    Checked exceptions like IOException should be handled or declared. Here, handling internally means try-catch inside method.
  2. Step 2: Understand unchecked exceptions handling

    Unchecked exceptions (RuntimeExceptions) usually indicate bugs and should propagate to caller, so do not catch them here.
  3. Final Answer:

    Use try-catch for IOException inside method; do not catch RuntimeExceptions. -> Option A
  4. 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
  • Ignoring exception handling rules