0
0
PHPprogramming~5 mins

Finally block behavior in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the finally block in PHP?
The finally block contains code that always runs after try and catch blocks, regardless of whether an exception was thrown or caught.
Click to reveal answer
beginner
When does the code inside a finally block execute?
It executes after the try block finishes, whether or not an exception was thrown or caught, and even if there is a return statement inside try or catch.
Click to reveal answer
intermediate
Can the finally block override a return statement in the try or catch blocks?
Yes, if the finally block contains a return statement, it will override any previous return from try or catch.
Click to reveal answer
advanced
What happens if an exception is thrown inside the finally block?
If an exception is thrown inside finally, it will override any exception thrown in the try or catch blocks and propagate up the call stack.
Click to reveal answer
beginner
Is the finally block mandatory in PHP exception handling?
No, the finally block is optional. You can use try with catch alone, but finally is useful for cleanup code that must always run.
Click to reveal answer
When does the finally block run in PHP?
AOnly if an exception is thrown
BAlways after <code>try</code> and <code>catch</code>, no matter what
COnly if no exception is thrown
DOnly if there is a <code>return</code> statement
What happens if finally contains a return statement?
AIt only runs if no exception was thrown
BIt is ignored
CIt overrides any previous <code>return</code> from <code>try</code> or <code>catch</code>
DIt causes a syntax error
If an exception is thrown in finally, what happens?
AIt replaces any exception from <code>try</code> or <code>catch</code>
BIt is ignored
CIt causes the program to crash immediately
DIt is caught automatically
Is the finally block required in PHP exception handling?
ANo, it is optional
BYes, always required
COnly if you use <code>catch</code>
DOnly if you use <code>throw</code>
Which of these is a good use for finally?
AReturning a value
BThrowing a new exception
CCatching an exception
DClosing a file or database connection
Explain how the finally block behaves in PHP exception handling.
Think about what happens no matter what in a try-catch-finally.
You got /5 concepts.
    Describe what happens if there is a return statement in both the try block and the finally block.
    Consider which return value the function actually sends back.
    You got /3 concepts.