try { // some code that may throw an exception } catch (Exception $e) { echo "Exception on line " . $e->getLine(); }
class CustomException extends Exception {} function throwError() { throw new CustomException("Something went wrong", 42); } try { throwError(); } catch (CustomException $e) { echo "Custom Exception on line " . $e->getLine(); }In this example, we define a custom exception class `CustomException` that extends the built-in `Exception` class. We then define a function `throwError()` that throws an instance of the `CustomException` class with a message and a line number (42 in this case). We call this function within a `try` block and catch the `CustomException` thrown by the function. We then output the line number using `$e->getLine()`. Package/library: PHP Standard Library (built-in to PHP)