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)

2
  • You'll need a distinct handler for exceptions and errors. Parsing errors usually cannot be catched however. Commented Feb 15, 2011 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. Commented Feb 15, 2011 at 20:17

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.