Exceptions: Difference between revisions

No edit summary
Line 625:
 
===Throwing an exception===
{{works with|Python|2.x and 3.x}}
Creating an exception using the default constructor of an exception class:
<lang python>def spam():
raise SillyError, 'egg'# equivalent to raise SillyError()</lang>
 
{{works with|Python|2.5}}
<lang python>def spam():
Passing an argument to the constructor of an exception class:
raise SillyError, 'egg'</lang>
<lang python>def spam():
raise SillyError, 'egg' # equivalent to raise SillyError('egg')</lang>
The above syntax is removed in Python 3.0; but the following syntax works in Python 2.x and 3.x, so should be preferred.
 
{{works with|Python|2.x and 3.x}}
<lang python>def spam():
raise SillyError('egg')</lang>
 
===Handling an exception===
Anonymous user