A
A
Architecktor1332017-09-04 11:13:54
PHP
Architecktor133, 2017-09-04 11:13:54

Can try-catch be used this way?

For example, I have this code:

function foo() {

    # Начало транзакции

    try {
        # Одно обращение к БД
    } catch(PDOException $e) {
        throw new PDOException("Уточняющее сообщение: " . $e->getMessage());
    }

    try {
        # Другое обращение к БД
    } catch(PDOException $e) {
        throw new PDOException("Другое уточняющее сообщение: " . $e->getMessage());
    }

    # Коммит транзакции

}

To what extent is such a construction legal, and is it used at all?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2017-09-04
@alexey-m-ukolov

It is better to create your own exception class hierarchy and throw them to make the code more readable, but in general, refining exceptions is a normal practice.

F
FanatPHP, 2017-09-04
@FanatPHP

Can be used. But in 99% of cases it is not needed.
In this case, most likely. Expecting an exception in a transaction is bad practice. Most likely, either no refinements are needed, or the data was not validated before the start of the transaction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question