0
0
Javaprogramming~10 mins

Exception propagation in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare that the method throws an exception.

Java
public void readFile() [1] IOException {
    // code to read file
}
Drag options to blanks, or click blank then click option'
Athrows
Bthrow
CthrowsException
DthrowsError
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'throw' instead of 'throws' in method declaration.
Misspelling the keyword.
2fill in blank
medium

Complete the code to propagate the exception from methodB to methodA.

Java
public void methodA() [1] Exception {
    methodB();
}

public void methodB() [1] Exception {
    throw new Exception("Error");
}
Drag options to blanks, or click blank then click option'
AthrowsException
Bthrow
CthrowsError
Dthrows
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'throw' instead of 'throws' in method declarations.
Not declaring the exception in methodA.
3fill in blank
hard

Fix the error in the code by completing the method signature to propagate the exception.

Java
public void process() [1] IOException {
    FileReader file = new FileReader("file.txt");
}
Drag options to blanks, or click blank then click option'
Athrow
Bthrows
CthrowsException
DthrowsError
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Not declaring the exception causes a compile error.
Using 'throw' instead of 'throws'.
4fill in blank
hard

Fill both blanks to propagate the exception from methodC to methodD.

Java
public void methodD() [1] Exception {
    methodC();
}

public void methodC() [2] Exception {
    throw new Exception("Failure");
}
Drag options to blanks, or click blank then click option'
Athrows
Bthrow
CthrowsException
DthrowsError
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'throw' instead of 'throws'.
Not declaring the exception in methodD.
5fill in blank
hard

Fill all three blanks to propagate exceptions correctly in nested method calls.

Java
public void start() [1] IOException {
    execute();
}

public void execute() [2] IOException {
    read();
}

public void read() [3] IOException {
    throw new IOException("Read error");
}
Drag options to blanks, or click blank then click option'
Athrow
Bthrows
CthrowsException
DthrowsError
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'throw' instead of 'throws'.
Not declaring the exception in all methods.