Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Throwing and Catching Exceptions in PHP

    Lets say I have the following code

    $test = preg_match_all($regular_expression, $string, $blah);

    If my regular expression is incorrect, it will display a "missing delimiter" error or a "out of range" error. How do I use exceptions in PHP so that I can catch the exception and display a unique message without seeing the error message?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Errors can be overridden to throw exceptions instead of triggering errors. You can wrap a custom function to throw an ErrorException instead:
    PHP Code:
    function errorExceptionHandler($errno$errstr$errfile$errline)
    {
        throw new 
    ErrorException($errstr0$errno$errfile$errline);
    }

    set_error_handler('errorExceptionHandler'); 
    Error handling cannot be done on any core or fatal error. If there is a problem with the interpretation of the code, there is no way to override an error handler.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •