Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another angle on this is returning an Exception, NOT throwing one, from the processing code.</p> <p>I needed to do this with a templating framework I'm writing. If the user attempts to access a property that doesn't exist on the data, I <em>return</em> the error from deep within the processing function, rather than throwing it.</p> <p>Then, in the calling code, I can decide whether to throw this returned error, causing the try() to catch(), or just continue:</p> <pre><code>// process the template try { // this function will pass back a value, or a TemplateExecption if invalid $result = $this-&gt;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; </code></pre> <p>The result of this is I still get the context of the original error, even though it was thrown at the top.</p> <p>Another option might be to return a custom NullObject or a UnknownProperty object and compare against that before deciding to trip the catch(), but as you can re-throw errors anyway, and if you're fully in control of the overall structure, I think this is a neat way round the issue of not being able to continue try/catches.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload