Do I want to make an exception or mistake in this PHP script?

I have a PHP script that runs database queries. Now, if the request fails, should I throw an error or throw an exception? I noticed that if I do the latter, the execution of the script will stop after the exception is thrown.

My code is as follows:

if (!$this->connection[0]->query($this->query))
    throw new Exception($this->connection[0]->error);

What are the pros and cons of using exceptions for such cases (failed requests)?

0
source share
4 answers

depends on your overall strategy for handling errors and requests passed to this function. The exception of the exceptions themselves is a very good idea, IMHO , if they are caught and processed somewhere.

+2

( )?

:

  • : , , , .
  • : .

, , . , , . , , .

, , - . , , , .

: ( ), , ( ), , .

+3

, , , . , , .

, , , / . (try..catch ..).

.

+1
source

If this is for an external website, I try to handle errors in detail during the development phase. After the site is ready to work live, I try not to give too detailed information to the end user about errors, especially about the details of the database for security reasons.

This is not some kind of answer set in stone, but remember about security when reporting and processing errors on external sites. Just a note, as this may not be an external website.

0
source

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


All Articles