📖 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
1
Create initial variables
Create a variable called $paymentStatus and set it to 'pending'. Also create a variable called $errorOccurred and set it to true.
PHP
Hint
Use = to assign values to variables. Strings need quotes.
2
Add try, catch, and finally blocks
Add a try block that throws an Exception with the message 'Payment failed' if $errorOccurred is true. Add a catch block to catch the Exception and set $paymentStatus to 'failed'. Add a finally block that sets $cleanupDone to true.
PHP
Hint
Use throw new Exception('message') inside try. Catch with catch (Exception $e). Use finally for cleanup.
3
Print the results
Print the values of $paymentStatus and $cleanupDone using echo. Separate them with a space.
PHP
Hint
Use echo to print. Use . to join strings. Convert boolean to string with a ternary.
4
Test with no error
Change the value of $errorOccurred to false and run the same code again. Print the values of $paymentStatus and $cleanupDone separated by a space.
PHP
Hint
Change $errorOccurred to false to avoid the exception.
Practice
(1/5)
1. What is the main purpose of the finally block in PHP exception handling?
easy
A. To stop the script execution immediately
B. To catch exceptions thrown in the try block
C. To execute code regardless of whether an exception was thrown or caught
D. To define the code that might throw an exception
Solution
Step 1: Understand the roles of try, catch, and finally
The try block contains code that may throw exceptions, catch handles those exceptions, and finally runs code after both regardless of exceptions.
Step 2: Identify the unique role of finally
The finally block always executes, even if an exception is not thrown or is caught, making it ideal for cleanup.
Final Answer:
To execute code regardless of whether an exception was thrown or caught -> Option C
Quick Check:
finally always runs = D [OK]
Hint: finally always runs after try/catch blocks [OK]
Common Mistakes:
Confusing finally with catch block
Thinking finally only runs on exceptions
Assuming finally stops script execution
2. Which of the following is the correct syntax for using a finally block in PHP?