try { $result = 10/0; } catch (Exception $e) { echo "Exception: " . $e->getMessage(); }
class CustomException extends Exception { public function errorMessage() { $errorMsg = 'Error on line ' . $this->getLine() . ' in ' . $this->getFile() . ': ' . $this->getMessage(); return $errorMsg; } } try { throw new CustomException('This is an exception'); } catch (CustomException $e) { echo $e->errorMessage(); }In this example, we create a custom exception by extending the Exception class. We add a method to return a custom error message with the line number and file name. Then we throw the custom exception and catch it in a block. We call the errorMessage() method to print out a custom error message. Package library: PHP Standard Library (SPL)