6

I was wondering how would one go about writing custom exception handlers.

so that I can do something like

throw new dbException($sql, $message);

and have it output

There was an error in your query Message: {$message here}
Query: {$sql here}
Line: {line exception was thrown on}
File: {file exception was thrown from}

but I also want to to catch eg syntax errors and parse errors (if possible)

Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • You'll need a distinct handler for [exceptions](http://php.net/manual/en/function.set-exception-handler.php) and [errors](http://www.php.net/manual/en/function.set-error-handler.php). Parsing errors usually cannot be catched however. – mario Feb 15 '11 at 20:17
  • Syntax/Parse errors are uncatchable, because they occur while php is being tokenized (by the lexer) and not in runtime. You can however, catch fatal errors (E_USER_ERROR) but it is your last chance to log any information before the script is being halted. – ludesign Feb 15 '11 at 20:17

3 Answers3

10

Well, you can extend the Exception class however you like. For custom exceptions, you might want to check out the post:

You should also find this thread useful:

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
2

Unless I am misunderstanding your question, you should be able to extend PHP's Exception class.

Sean Walsh
  • 8,266
  • 3
  • 30
  • 38
1

Why don't use just write your own exception class derived from the standard base exception? See extending exceptions manual.