Finally Block Behavior
📖 Scenario: Imagine you are writing a PHP script that processes a payment. You want to make sure that some cleanup code always runs, no matter if the payment succeeds or fails.
🎯 Goal: Build a PHP script that uses a try, catch, and finally block to handle an error and always run cleanup code.
📋 What You'll Learn
Create a variable
$paymentStatus with the value 'pending'.Create a variable
$errorOccurred with the value true.Use a
try block that throws an Exception with the message 'Payment failed' if $errorOccurred is true.Use a
catch block to catch the Exception and set $paymentStatus to 'failed'.Use a
finally block to set $cleanupDone to true.Print the values of
$paymentStatus and $cleanupDone.💡 Why This Matters
🌍 Real World
In real payment systems, you must always clean up resources like closing database connections or logging, even if an error happens.
💼 Career
Understanding exception handling and finally blocks is important for writing reliable PHP code in web development and backend services.
Progress0 / 4 steps