Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Always make the <code>try</code> block as small as possible.</p></li> <li><p>Only catch the exceptions you want to handle. Look in the documentation of the functions you are dealing with.</p></li> </ol> <p>This ensures that you think about what exceptions may occur, and you think about what to do if they occur. If something happens you never thought about, chances are your exception handling code won't be able to correctly deal with that case anyway, so it would be better the exception gets propagated.</p> <p>You said you'd "hate to have to list every single conceivable exception type", but usually it's not that bad. Opening a file? Catch <code>IOError</code>. Dealing with some library code? They often have their own exception hierarchies with a specific top-level exception -- just catch this one if you want to catch any of the library-specific exceptions. Be as specific as possible, otherwise errors will pass unnoticed sooner or later.</p> <p>As for convention about user-defined exceptions in Python: They <a href="http://docs.python.org/tutorial/errors.html#user-defined-exceptions" rel="noreferrer">usually should be derived from <code>Exception</code></a>. This is also what most user-defined exceptions in the standard library derive from, so the least you should do is use</p> <pre><code>except Exception: </code></pre> <p>instead of a bare <code>except</code> clause, which also catches <code>KeyboardInterrupt</code> and <code>SystemExit</code>. As you have noted yourself, this would still catch a lot of exceptions you don't want to catch.</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. 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