0
0
Javaprogramming~5 mins

Finally block in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of a finally block in Java?
The finally block is used to execute important code such as cleanup actions, regardless of whether an exception was thrown or caught.
Click to reveal answer
beginner
When does the code inside a finally block run?
It runs after the try and catch blocks finish, no matter if an exception occurred or not.
Click to reveal answer
intermediate
Can a finally block run if there is a return statement in the try block?
Yes, the finally block still runs even if the try block has a return statement.
Click to reveal answer
advanced
What happens if an exception is thrown inside a finally block?
If an exception occurs in the finally block, it can override any exception thrown in the try or catch blocks.
Click to reveal answer
beginner
Is a finally block mandatory after a try block?
No, a finally block is optional. Every try block must be followed by at least one catch or finally block.
Click to reveal answer
What is guaranteed by the finally block in Java?
AIt always executes after try/catch blocks, even if an exception occurs.
BIt only executes if an exception is caught.
CIt executes before the try block.
DIt executes only if there is no exception.
If a return statement is in the try block, what happens to the finally block?
AThe finally block is skipped.
BThe finally block runs before returning.
CThe program crashes.
DThe finally block runs only if an exception occurs.
Can a finally block be used without a catch block?
AYes, finally can be used alone with try.
BNo, catch is required.
COnly if there is no exception.
DOnly in Java 8 and later.
What happens if an exception is thrown inside a finally block?
AIt is ignored.
BIt is caught automatically.
CIt causes the program to exit immediately.
DIt replaces any previous exception.
Which of these is NOT true about finally blocks?
AThey always execute unless the JVM exits.
BThey can be used to close resources.
CThey can prevent exceptions from propagating.
DThey run even if there is a return in try.
Explain the role of the finally block in Java exception handling.
Think about what code you want to run no matter what happens in try or catch.
You got /4 concepts.
    Describe what happens when an exception is thrown inside a finally block.
    Consider how exceptions in finally affect the original exception.
    You got /3 concepts.