4

I started using "real" Exceptions instead of custom error-functions.

I think I don't need a try/catch-block everytime and its okay just to throw an exception but now I get an fatal error because of these uncaught exceptions.

Everything works fine when I set error_reporting(0) but I want to avoid errors at all. Does anyone know an alternative to try/catch or how to throw an exception without getting an fatal error?

Thanks in advance!

3
  • 2
    Uh ... no. The whole point of an exception is that if not caught it's always fatal. You shouldn't need try/catch blocks all over the place. You should set up a "last chance" exception handler with set_exception_handler Commented Nov 14, 2012 at 1:58
  • 1
    Is this a web or command-line application? You should be catching exceptions wherever you can handle them meaningfully. In some cases that's in the application's entry point, but many times it's deeper in where you want to take some action in response. Commented Nov 14, 2012 at 2:03
  • @David: its an web-application. Of course I try to catch them but e.g.: in my DB-Class I have a method called _handle_error() which handles the errors and logs them. When calling _handle_error() I cannot catch the exception thrown in the method itself. I hope its understandable without posting code. :-) Commented Nov 14, 2012 at 2:12

1 Answer 1

6

You can use set_exception_handler() for that and handle any uncaught exceptions yourself.

The callback that you register will receive the exception as its first and only argument. Registering a dummy function is possible, however:

  1. In a production environment it's recommended to log the exception instead of muffling it; this way you can keep track of exceptions that you didn't expect.

  2. The script execution halts after your handler is done.

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

2 Comments

Great, thanks! I just set an empty function without content as exception_handler and there's no fatal error anymore. I hope this isn't too quick & dirty..
Good to know, I've added two things to keep in mind when doing so.

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.