Junior — Middle
How are exception handling and interception implemented in your programming language?
sobes.tech AI
Answer from AI
In PHP, the try-catch-finally construct is used to catch and handle exceptions. The try block contains code that may throw an exception. If an exception occurs, control is transferred to the corresponding catch block, where the error can be handled. The finally block is executed always, regardless of whether an exception was thrown or not.
Example:
try {
// Code that may throw an exception
if (!file_exists("file.txt")) {
throw new Exception("File not found");
}
// Further processing
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
} finally {
echo " - Processing complete.";
}