class MyError extends Error { public function __construct($message) { parent::__construct($message); } } // Throw the error throw new MyError("Oops! Something went wrong.");
try { // Some code that may trigger an error } catch (Error $e) { // Handle the error echo "Error: " . $e->getMessage(); }In this example, we use a try-catch block to catch any errors that may occur while executing the code. If an error occurs, it is caught as an instance of the base PHP Error class and we can handle it in the catch block by accessing the error message using the getMessage() function.