D
D
dev4002016-06-29 22:19:16
PHP
dev400, 2016-06-29 22:19:16

What's the best way to deal with an exception?

Let's say there are no categories and the model threw an exception

public function allCategories() {
        while(!$rows = parent::findAll()){
            throw new \Exception('Нет категорий');
        }
        return $rows;
    }

Show it to the user, or make a more informative exception and write it to a file, and the user generate an error message?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
sts, 2016-06-29
@stunoff

And write everything down for yourself, and tell the user that he is trying to look for something wrong

O
OnYourLips, 2016-06-29
@OnYourLips

Issue a beautiful warning that the site is down and under maintenance, pledge.

A
Andrey Burov, 2016-06-29
@BuriK666

while(!$rows = parent::findAll()){
    throw new \Exception('Нет категорий');
}
return $rows;

maybe better
if (($rows = parent::findAll()) === false);
    throw new \Exception('Нет категорий');
}
return $rows;

Show the user a normal message that there are no categories. In this particular case, I think you still found some categories at the request of the user or not.

D
dmitriy, 2016-06-29
@dmitriylanets

do you really need an exception here? maybe just return null ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question