try { // some code here that may throw an exception } catch(Exception $e) { $errorMsg = $e->getMessage(); $errorCode = $e->getCode(); $exception = new Exception($errorMsg, $errorCode); // handle the exception }
class CustomException extends Exception { public function __construct() { parent::__construct("Custom error occurred", 500); } } try { throw new CustomException(); } catch(Exception $e) { $errorMsg = $e->getMessage(); $errorCode = $e->getCode(); $exception = new Exception($errorMsg, $errorCode); // handle the exception }In this example, we are extending the Exception class to create a custom exception that always has the same message and error code. We then throw this custom exception and handle it as before. Both examples use the core PHP library for exceptions.