Answer the question
In order to leave comments, you need to log in
Exception handling with dispatch?
I'm writing some code in PHP (however, the question applies to any programming language that supports exceptions). From the point of view of error handling, there is a suggestion to do the following:
1) Translate any PHP errors and warnings into exceptions using set_error_handler
2) Throw your own exceptions in cases where it is required by the program logic
3) Divide all exceptions into fatal , logged and ignored . Which category the exception belongs to is determined by the type of exception.
4) As a centralized exception handler via set_error_handlerassign some method that will decide which category the exception belongs to and perform one of the following actions:
a) For fatal exceptions, throw it again.
b) For logged exceptions, log an entry and continue.
c) For ignored exceptions, just keep going.
The logic for determining the type of an exception can be changed through the program configuration, so you can make all exceptions fatal or make some exceptions ignored. The problem arises with the continuation of work. In order for any function to continue its work after throwing an exception, you need to write try-catch blocks everywhere. This defeats the purpose of assigning a centralized exception handler. The only option I see is something like this:
function helloWorld(){
try{
//Что-нибудь, кидающее исключение
} catch (Exception $e) {
Handler::handle($e);
}
}
Answer the question
In order to leave comments, you need to log in
In general, the idea is not very good. The same exception can be fatal in one case and ignored in another. (By the way, another processing option is throwing a new exception.) Whether or not to write an exception to the log depends on the logging mode - when debugging, write everything, during normal work, write only the important. Well, yes, the point of using exceptions is lost if in each function you have try/catch
.
The usual approach is for a function to catch and handle only those exceptions that it actually expects and can handle. Handle means to ignore (usually doing something else), or redefine and throw a new, more precise exception. If the function simply throws the exception above, then this is not processing, it is the same as if it try/catch
were not there at all.
Let's say one function reads a file; if the file does not exist, it throws an exception, because it is a simple function and cannot do anything smart in this situation. And the calling function might already be doing something, like creating a new file with default values. Or redefine it - not just “I can’t open the file”, but “I can’t initialize such and such a plugin, reinstall it”. If it is a non-critical plugin, then the function above will have another function that will handle this exception as well (ignore, but notify the user).
At the very top is a universal handler that writes down all the exceptions that have reached it, and then either exits or, say, displays a message for the user (although I don’t know if this applies to PHP).
First, you are confusing the terms "PHP error" and "exception" in the text. In particular, the exception cannot be ignored and program execution can continue.
The idea of wrapping everything in try / catch blocks is also insanity, exceptions were prizhuman just to avoid the need to check the result of all functions.
Here's how to do it right:
1) Do set_error_handler, which turns all errors/warnings/notices/strict standards into exceptions
2) Set error_reporting(-1)
3) Do set_exception_handler(), which logs the exception (there are no unimportant exceptions) and displays a beautiful 503 page (not in production, error details are immediately displayed).
4) After a while we get the application without errors
The application must not ignore errors and exceptions, as in this case it may give incorrect data to the user. it is better to give an error page than incorrect data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question