Re: [revised RFC] PEAR error handling
| From: | Greg Beaver | Date: | Fri, 29 Aug 2003 00:56:46 +0000 |
| Subject: | Re: [revised RFC] PEAR error handling | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-20788@lists.php.net to get a copy of this message | ||
Krystof Slaby wrote:
GB> 6. Only errors and exceptions should be returned. GB> If an error must GB> terminate the program execution, it is an exception. What about exception handling? Exception terminates from program block but doesn't have to terminate whole program when properly catched. In php4 language, there are no exceptions of course - but I think this was meant generally. Yes - this is the responsibility of error handling. The package shouldn't care. An exception is an error that can't be corrected by the package without external help (better definition)
GB> If an error must GB> terminate the current function, but not the program execution, it is an GB> error. Does 'error code' (non-OK return value) returned from function which was not terminated mean error? Basically, the difference is really between errors that terminate and errors that only note an error condition.Here is a warning or notice: <?php $randomcode = 'random string'; if ($something) { Error_Raise::warning('mypackage', MYPACKAGE_ERROR_CODE); } $othercodethatisexecuted = "wheeee"; ?> Notice - no change of program flow. It is up to the error handler to catch the warning. Here is an error or exception: <?php $randomcode = 'random string'; if ($something) { return Error_Raise::error('mypackage', MYPACKAGE_ERROR_CODE); } $othercodethatmightbeexecuted = "wheee"; ?> Note that the only difference is whether the error class terminates the current execution block, or simply notes a possible error condition before continuing. The difference between exception and error is a bit arbitrary, except that if PHP 5 is present, an exception will be thrown instead of handled by an error callback. In PHP 5, it should be noted that throw() does not return to the next line of execution. In other words, throw() is nothing like a function call, it's closer to a goto statement. So, exceptions can not handle warnings or notices, which is the only reason that my error handling code will still be necessary even in PHP 5. Regards, Greg