Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I usually prefer to handle exceptions internally (i.e. try/except inside the called function, possibly returning a None) because python is dynamically typed. In general, I consider it a judgment call one way or the other, but in a dynamically typed language, there are small factors that tip the scales in favor of not passing the exception to the caller:</p> <ol> <li>Anyone calling your function is not notified of the exceptions that can be thrown. It becomes a bit of an art form to know what kind of exception you are hunting for (and generic except blocks ought to be avoided).</li> <li><code>if val is None</code> is a little easier than <code>except ComplicatedCustomExceptionThatHadToBeImportedFromSomeNameSpace</code>. Seriously, I hate having to remember to type <code>from django.core.exceptions import ObjectDoesNotExist</code> at the top of all my django files just to handle a really common use case. In a statically typed world, let the editor do it for you.</li> </ol> <p>Honestly, though, it's always a judgment call, and the situation you're describing, where the called function receives an error it can't help, is an excellent reason to re-raise an exception that is meaningful. You have the exact right idea, but unless you're exception is going to provide more meaningful information in a stack trace than</p> <pre><code>AttributeError: 'NoneType' object has no attribute 'foo' </code></pre> <p>which, nine times out of ten, is what the caller will see if you return an unhandled None, don't bother.</p> <p>(All this kind of makes me wish that python exceptions had the <code>cause</code> attributes by default, as in java, which lets you pass exceptions into new exceptions so that you can rethrow all you want and never lose the original source of the problem.)</p>
    singulars
    1. This table or related slice is empty.
    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.
    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