Complete the code to ensure the finally block always executes.
<?php try { echo "Try block executed.\n"; } [1] { echo "Catch block executed.\n"; } finally { echo "Finally block executed.\n"; } ?>
The catch (Exception $e) block catches exceptions and allows the finally block to execute afterward.
Complete the code to throw an exception inside the try block.
<?php try { throw new [1]("Error occurred."); } catch (Exception $e) { echo $e->getMessage(); } finally { echo "\nFinally block runs."; } ?>
Throwing a new Exception inside the try block triggers the catch block.
Fix the error in the catch block syntax.
<?php try { echo "Inside try.\n"; } catch [1] { echo "Caught exception.\n"; } finally { echo "Finally block executed.\n"; } ?>
The catch block must specify the exception type and a variable to hold it, like (Exception $e).
Fill both blanks to create a finally block that always runs after try and catch.
<?php try { echo "Trying...\n"; } catch (Exception $e) { echo "Exception caught.\n"; } [1] { echo "[2] block executed.\n"; } ?>
The finally block runs after try and catch blocks regardless of exceptions.
Fill all three blanks to handle an exception and ensure the finally block runs.
<?php try { throw new [1]("Error!"); } catch ([2] $e) { echo $e->getMessage(); } [3] { echo "\nFinally block executed."; } ?>
Throw an Exception, catch it with Exception $e, and use finally to run code always.
