Re: Error handling brainstorming

From: Date: Thu, 09 Aug 2012 05:33:01 +0000
Subject: Re: Error handling brainstorming
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-62095@lists.php.net to get a copy of this message
Hi!
Because checking that the returned variable is !== FALSE is *way* better than throwing an exception, right?
Yes, it is. You can control it, unlike the exception which you can not, unless, again, you wrap everything into try/catch on every kind of exception possible.
Have you stopped for a moment to think this opinion through? Look at two typical patterns of error handling. The examples below are generalized from my file cache code. Having no cache is exceptional, because 99.999% of the time I run this, there is cache, except the first time when there's not. Tell me again how you using try..catch is very verbose and how you need to supply multiple catch blocks for "every kind of exception possible"? With exceptions: ---------------- try { fileop1(); fileop2(); fileop3(); normal_actions(); } catch (IOException $e) { exceptional_actions(); } Without exceptions: -------------------- $valid = true; if (fileop1() !== FALSE) { $valid = false; } if ($valid && fileop2() !== FALSE) { $valid = false; } if ($valid && fileop3() !== FALSE) { $valid = false; } if ($valid) { normal_actions(); } else { exceptional_actions(); }

« previous php.internals (#62095) next »