E
E
Evgeny Musonov2020-02-10 10:22:04
PHP
Evgeny Musonov, 2020-02-10 10:22:04

How to catch exceptions in multiple methods?

There are two methods that return an object and, accordingly, can be called by a chain (the call goes in the controller), for example
$object->upload()->resize();
Each of the methods contains its own exceptions. Now I have a try-catch block in each method.

public function first()
{
    try {

    } catch {

    }
}

public function second()
{
    try {

    } catch {

    }
}

What is the best way to move the try-catch block outside the methods, but not to insert the try-catch in the controller?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-02-10
@evmusonov

The question is strange. Which outlet should I plug the kettle into? The one closer!
It is correct to write a try-catch block where you need to catch an exception.
If you need to catch an exception in the controller, catch it in the controller.
If it is necessary to catch in a method - to catch in a method.
If you need to catch any exception in the program - either in the global try-catch block or in the exception handler.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question