📖 Scenario: Imagine you are writing a PHP script that processes user input for a simple calculator. The input might cause different types of errors, such as division by zero or invalid input format. You want to handle these errors gracefully using multiple catch blocks.
🎯 Goal: Build a PHP script that uses try and catch blocks to handle different exceptions separately. You will create a function that divides two numbers and catches DivisionByZeroError and TypeError with different messages.
📋 What You'll Learn
Create a function called divide that takes two parameters: $a and $b.
Inside the function, perform the division $a / $b inside a try block.
Use multiple catch blocks to handle DivisionByZeroError and TypeError exceptions separately.
Print different error messages for each exception type.
Call the divide function with different inputs to test the error handling.
💡 Why This Matters
🌍 Real World
Handling different errors separately helps make programs more reliable and user-friendly, especially when dealing with user input or external data.
💼 Career
Understanding multiple catch blocks is important for writing robust PHP applications that can gracefully handle various runtime errors.
Progress0 / 4 steps
1
Create the divide function with parameters
Write a PHP function called divide that takes two parameters: $a and $b. Inside the function, start a try block but do not add any code inside it yet.
PHP
Hint
Define the function with function divide($a, $b) and add a try block inside it.
2
Add division inside the try block
Inside the try block of the divide function, write code to return the result of $a / $b.
PHP
Hint
Use return $a / $b; inside the try block.
3
Add multiple catch blocks for error handling
Add two catch blocks after the try block in the divide function. The first catch should catch DivisionByZeroError and print "Error: Division by zero.". The second catch should catch TypeError and print "Error: Invalid input type.".
PHP
Hint
Use two catch blocks: one for DivisionByZeroError and one for TypeError, each printing the correct message.
4
Call the divide function and print results
Call the divide function three times with these inputs: 10, 2, 5, 0, and "a", 3. Print the result or error message for each call on a new line using echo.
PHP
Hint
Call divide(10, 2), divide(5, 0), and divide("a", 3). Print each result followed by a newline.
Practice
(1/5)
1.
What is the main purpose of using multiple catch blocks in PHP?
easy
A. To handle different types of exceptions separately
B. To run multiple try blocks at once
C. To catch errors without specifying the type
D. To speed up the program execution
Solution
Step 1: Understand the role of catch blocks
Catch blocks are used to handle exceptions that occur in the try block.
Step 2: Recognize the benefit of multiple catch blocks
Multiple catch blocks allow handling different exception types in different ways, improving error management.
Final Answer:
To handle different types of exceptions separately -> Option A
Quick Check:
Multiple catch blocks = different exception handling [OK]
Hint: Multiple catch blocks handle specific exceptions separately [OK]
Common Mistakes:
Thinking multiple catch blocks run in parallel
Believing catch blocks catch all errors without type
Assuming multiple catch blocks improve speed
2.
Which of the following is the correct syntax to catch multiple exceptions in PHP?
PHP uses separate catch blocks for each exception type, each with its own parentheses.
Step 2: Identify the correct syntax
try { } catch (ExceptionType1 $e) { } catch (ExceptionType2 $e) { } shows two separate catch blocks, which is the correct way to catch multiple exceptions.