Re: Error handling brainstorming

From: Date: Thu, 09 Aug 2012 06:46:49 +0000
Subject: Re: Error handling brainstorming
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-62098@lists.php.net to get a copy of this message
You are either purposefully exaggerating or not doing it right. if(fileop1() && fileop2() && fileop3()) { // do valid stuff } else { // do error stuff } It's not that hard.
I guess it was my mistake that I simplified my actual code for simplicity's sake. Please show me how would my actual code look stuffed in an "if" statement: try { $cacheBaseFile = Core\CACHE_PATH . '/Compiled/' . \str_replace('\\', '-', $symbolName); // The validator checks whether the cache is fresh. $cacheIsFresh = include $cacheFileBase . '.val.php'; // Attempt to load the cache (it should be there if the validator is there, unless someone messed with the files). if ($cacheIsFresh) {
     $result = include $cacheFileBase . '.main.php';
} else {
     $this->generateCache($symbolName);
     ...
} return $result; } catch (IncludeIOError $e) { // One or more of the files were damaged or missing. ... } When you have: 1) comments 2) multiple statements 3) assign variables 4) call functions/methods with long arguments etc. ...you can't just stuff it in an if (op() && op() && op()) and pretend this is in any way a maintainable code style.
With exceptions: ---------------- try {
    fileop1();
    fileop2();
    fileop3();
    normal_actions();
} catch (IOException $e) {
    exceptional_actions();
}
Now imagine fileop1 throws some other exception - note that you have no idea which exceptions are there and nothing guarantees you fileop1 throws only IOException, and by the proposal here, any problem in any code whatsoever results in exception now, even if the problem was in converting log message to utf-8 while writing some log inside some function called by fileop2(). Now your whole app has failed, even though you don't really care about that log message at all.
Exceptions aren't about log messages, they're about exceptions - error conditions that have to be handled. If my code above generates another type of exception there are only two options: 1) Either I catch that exception because I know how to handle it -or- 2) I don't catch it, and the app should correctly stop right there before data damage is done. I know PHP's model is all messed up, but no one here, I believe, is asking about putting non-error log messages in Exceptions. IO failure is an exception. If your IO operation fails, you can't just log it and plow forward blissfully without handling the problem. Stan

« previous php.internals (#62098) next »