B
B
BonBon Slick2017-07-27 12:48:58
Exceptions
BonBon Slick, 2017-07-27 12:48:58

Is it good practice to use try/catch everywhere?

In my project, I use everywhere

try {
            DB::beginTransaction();
// code...
            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();
            return response()->json([
                'error' => true,
                'message' => $e->getMessage()
            ]);
        }

For any action, update, deletion, entry, exit and other actions.
Is it good practice to do this? Or should it be used only for CRUD actions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2017-07-27
@BonBonSlick

It depends on the language. In python, for example, raising an exception does not incur noticeable costs, and, accordingly, is the same means of controlling the flow of execution as a loop, if, etc. In C++, exceptions are more expensive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question