PHP exceptions - try / catch?

I started using "real" exceptions instead of custom error functions.

I think I don't need a try / catch block every time, and everything is fine, just to throw an exception, but now I get a fatal error due to these uncaught exceptions.

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

Thanks in advance!

+4
source share
1 answer

You can use set_exception_handler() to do this, and handle any uncaught exceptions yourself.

The callback you register will receive an exception as its first and only argument. However, registration of a dummy function is possible:

  • In a production environment, it is recommended to register an exception rather than drown it out; this way you can track exceptions that you did not expect.

  • The execution of the script stops after the handler completes.

+5
source

Source: https://habr.com/ru/post/1445904/


All Articles