A
A
Alex1222016-11-27 14:49:03
Yii
Alex122, 2016-11-27 14:49:03

How to design exception handling?

Hello! Now I am designing my framework. I used to write in yii2. I decided to implement exception handling in the core of the framework. I read this https://habrahabr.ru/post/30399/and a few more articles on exception handling. In practice, I began to write exception classes and throw them in the code. Meanwhile, when installing xdebug, you can already see a beautiful call stack in the table along with the error, the path to the file and the line where the error occurred. When I start throwing custom exceptions, not only do I need to write a catch block to handle it (otherwise an error about an uncaught exception is thrown - Uncaught exception), but also a beautiful stack label disappears and I have to create it himself (inventing the wheel). What conclusion did I draw: if I throw an exception somewhere (Exception or some of my own), then under it I must write a catch block, and invent a beautiful stack output again, respectively, if I have 20 of my custom exceptions, I will have to write 20 catch blocks (if my goal is to output a unique error message and function stack, then this is not rational). Moreover, if you do not throw an exception, then it will still give me an error, in a beautiful frame. I'm under the impression that exception handling is only beneficial when you really need to write some kind of handler (and not just display an error message and a stack). What is then the benefit of using exceptions in the framework, what are your thoughts? when you really need to write some kind of handler (and not just display an error message and a stack). What is then the benefit of using exceptions in the framework, what are your thoughts? when you really need to write some kind of handler (and not just display an error message and a stack). What is then the benefit of using exceptions in the framework, what are your thoughts?
Прошу не придераться к словам и указать на ошибки в моих рассуждениях, если они есть, спасибо.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zTrue, 2016-11-27
@Alex122

Скорее всего, у Вас есть общая точка входа, оберните весь код вызова приложения в try-catch и ловите там \Exception. Все кастомные исключения должны наследоваться от него, чтобы попасть в этот блок.

<?php
// index.php
try {
  $app = new \Framework\App();
  $app->run();
} catch (\Exception $e) {
  // handle any unhandled exception here
}

L
lega, 2016-11-27
@lega

Ловить конкретную ошибку имеет смысл если вы её ожидаете и знаете как её обработать. Иначе пусть она валится до самого верхнего catch, там логируется и шлет вам mail/сообщение в чат.
Далее вы уже решаете что с ней сделать - либо исправить ошибку, либо это будет ожидаемое исключение и вы для её добавите catch с обработчиком.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question