Errors and exceptions help PHP tell you when something goes wrong in your code. They let you find and fix problems easily.
Error vs Exception in PHP
<?php // Throwing an exception throw new Exception('Something went wrong'); // Catching an exception try { // code that may cause exception } catch (Exception $e) { echo $e->getMessage(); } // Triggering an error trigger_error('This is an error', E_USER_ERROR);
Exceptions are objects you can throw and catch to handle problems gracefully.
Errors are usually serious problems that stop the script unless handled.
<?php // Example of throwing and catching an exception try { throw new Exception('Oops!'); } catch (Exception $e) { echo 'Caught exception: ' . $e->getMessage(); }
<?php // Example of triggering an error trigger_error('Custom error happened', E_USER_WARNING);
<?php // Handling errors with set_error_handler set_error_handler(function($errno, $errstr) { echo "Error caught: $errstr"; }); trigger_error('Fatal error example', E_USER_ERROR);
This program shows how exceptions stop wrong operations like dividing by zero and how errors can warn without stopping.
<?php function divide($a, $b) { if ($b == 0) { throw new Exception('Division by zero'); } return $a / $b; } try { echo divide(10, 2) . "\n"; // Works fine echo divide(5, 0) . "\n"; // Throws exception } catch (Exception $e) { echo 'Error: ' . $e->getMessage() . "\n"; } // Trigger an error trigger_error('This is a user warning', E_USER_WARNING);
Exceptions let you control what happens when something goes wrong.
Errors are often more serious and may stop your script unless handled.
Use try-catch blocks to handle exceptions and set_error_handler for errors.
Errors and exceptions both show problems in PHP but work differently.
Exceptions can be caught and handled to keep your program running.
Errors usually indicate serious issues but can be managed with custom handlers.
Practice
Error and an Exception in PHP?Solution
Step 1: Understand PHP error handling basics
Errors usually indicate serious problems like syntax errors or memory issues that stop the script.Step 2: Understand exceptions in PHP
Exceptions are objects that can be caught and handled usingtry-catchblocks to keep the program running.Final Answer:
Errors are serious problems that usually stop the script, while exceptions can be caught and handled. -> Option AQuick Check:
Error stops script, Exception can be caught [OK]
- Thinking exceptions always stop the script
- Believing errors can be caught with try-catch
- Confusing errors and exceptions as identical
Solution
Step 1: Recall PHP exception handling syntax
PHP usestry { ... } catch (Exception $e) { ... }to handle exceptions.Step 2: Identify correct syntax among options
try { /* code */ } catch (Exception $e) { /* handle */ } matches the correct PHP syntax; others use wrong keywords or order.Final Answer:
try { /* code */ } catch (Exception $e) { /* handle */ } -> Option AQuick Check:
try-catch block syntax = try { /* code */ } catch (Exception $e) { /* handle */ } [OK]
- Using 'except' instead of 'catch'
- Placing catch before try
- Using colon syntax which is invalid in PHP
try {
throw new Exception('Oops!');
echo 'No error';
} catch (Exception $e) {
echo 'Caught: ' . $e->getMessage();
}Solution
Step 1: Understand the throw statement
The code throws an Exception with message 'Oops!'. The line after throw is not executed.Step 2: Catch block execution
The catch block catches the Exception and echoes 'Caught: ' plus the message.Final Answer:
Caught: Oops! -> Option CQuick Check:
Exception thrown and caught outputs message [OK]
- Expecting 'No error' to print
- Thinking throw causes fatal error without catch
- Ignoring that catch block runs after throw
try {
echo 10 / 0;
} catch (Exception $e) {
echo 'Error caught';
}Solution
Step 1: Understand division by zero in PHP
Division by zero triggers a fatal Error, not an Exception.Step 2: Check catch block behavior
Catch block only catches Exceptions, so the Error is not caught and script stops.Final Answer:
Division by zero error is not caught because it is an Error, not an Exception. -> Option BQuick Check:
Errors not caught by catch(Exception) [OK]
- Assuming catch catches all errors
- Thinking division by zero throws Exception
- Ignoring difference between Error and Exception
Error and Exception in PHP to prevent your script from stopping unexpectedly. Which approach is best?Solution
Step 1: Understand handling exceptions
Exceptions are handled withtry-catchblocks to catch and manage them.Step 2: Understand handling errors
Errors are handled by setting a custom error handler usingset_error_handler()to manage errors gracefully.Final Answer:
Use try-catch for exceptions and set a custom error handler with set_error_handler() for errors. -> Option DQuick Check:
Separate handlers for errors and exceptions [OK]
- Expecting try-catch to catch all errors
- Ignoring errors leading to script termination
- Using set_exception_handler for errors
Ready to prove it?
Pass the test and this pattern moves unknown
