J
J
JackShcherbakov2018-03-31 19:21:45
PHP
JackShcherbakov, 2018-03-31 19:21:45

Confusion with error handling and exceptions. How to do it right?

Hello colleagues! I recently went through the topic of PHP error handling and exceptions, and no matter how much I wanted to understand everything, there were still a couple of questions left.
So let's take as an example a situation where a script tries to open a non-existent file with fopen(). By default, the script will throw out a warning that I want to handle. Here I see 2 ways:

  1. suppress error with @
  2. or catch this same error with set_error_handler().

In both cases, we need to do 2 things:
  1. Handle error
  2. Notify the user
  3. If something is serious (for example, the inability to connect to the database), then write information about the error / exception to the appropriate log.

And then the questions arise:
  1. I do not quite understand how to correctly notify the user about the exceptions and errors that have arisen. You can't stupidly die("Error!!!") or something like that. And what to do with already generated content? After all, I do not want the user to see anything other than an error, because this request makes no sense.
  2. It is also not clear what to generate in the functions that I write - exceptions or errors?

I would like to thank everyone in advance who will help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2018-03-31
@JackShcherbakov

1. The "ERROR TRACKING SYSTEMS" tag has nothing to do with the question (if so, many tags have descriptions).
2. error suppression does not remove the error itself, and it is necessary to remove not the message about it, but the reason for its occurrence. specifically for fopen - it is enough to check the existence of a file / access to it.
3. set_error_handler and exceptions are different areas, sometimes absolutely.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question