Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Generally, you want to use condition checking for situations which are understandable, expected, and able to be handled. You would use exceptions for cases that are incoherent or unhandleable. </p> <p>So, if you think of your "add" function. It should NEVER return null. That is not a coherent result for adding two things. In that case, there is an error in the arguments that were passed in and the function should <em>not</em> attempt to pretend that everything is okay. This is a perfect case to throw an exception. </p> <p>You would want to use condition checking and return null if you are in a regular or normal execution case. For instance, <code>IsEqual</code> could be a good case to use conditions, and return false if one of your conditions fails. I.E.</p> <pre><code>function bool IsEqual(obj a, obj b) { if(a is null) return false; if(b is null) return false; if(a.Type != b.Type) return false; bool result = false; //Do custom IsEqual comparison code return result; } </code></pre> <p>In that scenario, you are returning false both for the exception cases AND the "objects are not equal case". This means that the consumer (calling party) cannot tell whether the comparison failed or the objects were simply <em>not equal</em>. If those cases need to be distinguished, then you should use exceptions instead of conditions.</p> <p>Ultimately, you want to ask yourself whether the consumer will be able to specifically handle the failure case that you encountered. If your method/function <em>cannot do what it needs to do</em> then you probably want to throw an exception. </p>
 

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