Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I remember correctly, when Python switched to the new raise syntax in 2.5(?), they got rid of the <code>message</code> member in favor of an <code>args</code> tuple. For backward compatibility, <code>BaseException.message</code> is effectively the same thing as <code>BaseException.args[0] if BaseException.args else None</code>, but you shouldn't use it in new code.</p> <p>So, change <code>message</code> to either <code>args</code> (if you want all args) or <code>args[0]</code> (or, if you're worried there may be no args, the fancier version that protects against <code>()</code>), depending on which you want.</p> <p>The reason for this change is that with the new-style exceptions, there's no magic to <code>raise</code> or <code>except</code> anymore; you're just calling the exception class's constructor in the <code>raise</code> statement, and catching the exception in a variable in the <code>except</code> statement. So:</p> <pre><code>try: raise MyException('Out of cheese error', 42) except Exception as x: print x.args </code></pre> <p>This will print <code>('Out of cheese error', 42)</code>. If you only had <code>print x.message</code> you'd just get <code>'Out of cheese error'</code>. So, the Exception subclasses that used to have to do fancy things to carry around an error code as a separate member, etc., can be simplified; in fact, the whole thing comes down to this:</p> <pre><code>class BaseException(object): def __init__(self, *args): self.args = args </code></pre>
 

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