0
0
Javaprogramming~10 mins

Checked vs unchecked exceptions in Java - Interactive Practice

Choose your learning style9 modes available
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.