Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should only catch exceptions that you will handle.</p> <p>Catching all exceptions by their concrete types is nonsense. You should catch specific exceptions you <em>can</em> and <em>will</em> handle. For other exceptions, you may write a generic catch that catches "base Exception", logs it (use <code>str()</code> function) and terminates your program (or does something else that's appropriate in a crashy situation).</p> <p>If you really gonna handle <em>all</em> exceptions and are sure none of them are fatal (for example, if you're running the code in some kind of a sandboxed environment), then your approach of catching generic BaseException fits your aims.</p> <p>You might be also interested in <a href="http://docs.python.org/library/exceptions.html#exceptions.BaseException" rel="noreferrer">language exception reference</a>, not a reference for the library you're using.</p> <p>If the library reference is really poor and it doesn't re-throw its own exceptions when catching system ones, <strong>the only useful approach is to run tests</strong> (maybe add it to test suite, because if something is undocumented, it may change!). Delete a file crucial for your code and check what exception is being thrown. Supply too much data and check what error it yields.</p> <p>You will have to run tests anyway, since, even <strong>if the method of getting the exceptions by source code existed, it wouldn't give you any idea how you should handle any of those</strong>. Maybe you should be showing error message "File needful.txt is not found!" when you catch <code>IndexError</code>? Only test can tell.</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