Complete the code to throw an exception in PHP.
<?php throw new [1]("Something went wrong!"); ?>
The Exception class is used to throw exceptions in PHP.
Complete the code to catch an exception in PHP.
<?php try { // code that may throw } catch ([1] $e) { echo $e->getMessage(); } ?>
The catch block catches exceptions of type Exception or its subclasses.
Fix the error in the code to properly handle both errors and exceptions.
<?php try { // code } catch ([1] $e) { echo $e->getMessage(); } ?>
The Throwable interface allows catching both Error and Exception types.
Fill both blanks to create a custom exception class that extends the base Exception.
<?php class MyException extends [1] { public function [2]() { return "Custom exception message."; } } ?>
Custom exceptions extend Exception and can override getMessage() to customize the message.
Fill all three blanks to throw and catch a custom exception properly.
<?php class CustomError extends [1] {} try { throw new [2](); } catch ([3] $e) { echo "Caught custom error."; } ?>
The custom exception class extends Exception. We throw an instance of CustomError and catch it by the same class.
