Another angle on this returns an Exception NOT throwing it from the processing code.
I needed to do this with the template I am writing. If the user tries to access a property that does not exist in the data, I return an error from the depth of the processing function, and do not throw it.
Then, in the calling code, I can decide whether to return the returned error by forcing try () catch () or just continue:
// process the template try { // this function will pass back a value, or a TemplateExecption if invalid $result = $this->process($value); // if the result is an error, choose what to do with it if($result instanceof TemplateExecption) { if(DEBUGGING == TRUE) { throw($result); // throw the original error } else { $result = NULL; // ignore the error } } } // catch TemplateExceptions catch(TemplateException $e) { // handle template exceptions } // catch normal PHP Exceptions catch(Exception $e) { // handle normal exceptions } // if we get here, $result was valid, or ignored return $result;
As a result, I still get the context of the original error, even if it was selected at the top.
Another option might be to return a custom NullObject or UnknownProperty object and compare it with before deciding to disable catch (), but since you can re-throw errors anyway, and if you have full control over the overall structure, I think this is a neat way around the problem of inability to continue trying / catching.
davestewart Aug 12 '13 at 19:35 2013-08-12 19:35
source share