Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>IMHO, it shouldn't be an exception at all. An exception, in my mind, should be used when exceptional things happen, and not as flow controls.</p> <p>In your case, it isn't at all an exceptional status that someone tries to transfer more money than the balance allows. I figure these things happen very often in the real world. So you should program against these situations. An exception might be that your if-statement evaluates the balance good, but when the money is actually being subtracted from the account, the balance isn't good anymore, for some strange reason.</p> <p>An exception might be that, just before calling <code>transferTo()</code>, you checked that the line was open to the bank. But inside the <code>transferTo()</code>, the code notices that the line isn't open any more, although, by all logic, it should be. <em>THAT</em> is an exception. If the line can't be opened, that's not an exception, that's a plausible situation.</p> <p>IMHO recap: Exceptions == weird black magic.</p> <p><strong><em>being-constructive-edit:</em></strong></p> <p>So, not to be all too contradictive, the method itself might very well throw an exception. But the use of the method should be controlled: You first check the balance (outside of the <code>transferTo()</code> method), and if the balance is good, only then call <code>transferTo()</code>. If <code>transferTo()</code> notices that the balance, for some odd reason, isn't good anymore, you throw the exception, which you diligently catch. </p> <p>In that case, you have all your ducks in a row, and know that there's nothing more you can do (because what was <code>true</code> became <code>false</code>, as if by itself), other than log the exception, send a notification to someone, and tell the customer politely that someone didn't sacrifice their virgins properly during the last full moon, and the problem will be fixed at the first possible moment.</p> <p><strong><em>less-enterprisey-suggestion-edit:</em></strong></p> <p>If you are doing this for your own pleasure (and the case seems to be this, see comments), I'd suggest returning a boolean instead. The usage would be something like this:</p> <pre><code>// ... boolean success = transferTo(otherAccount, ONE_MILLION_DOLLARS_EXCLAMATION); if (!success) { UI.showMessage("Aww shucks. You're not that rich"); return; // or something... } else { profit(); } // ... </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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