~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Doc/tutorial/errors.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
165
165
The except clause may specify a variable after the exception name (or tuple).
166
166
The variable is bound to an exception instance with the arguments stored in
167
167
``instance.args``.  For convenience, the exception instance defines
168
 
:meth:`__getitem__` and :meth:`__str__` so the arguments can be accessed or
169
 
printed directly without having to reference ``.args``.
 
168
:meth:`__str__` so the arguments can be printed directly without having to
 
169
reference ``.args``.
170
170
 
171
 
But use of ``.args`` is discouraged.  Instead, the preferred use is to pass a
172
 
single argument to an exception (which can be a tuple if multiple arguments are
173
 
needed) and have it bound to the ``message`` attribute.  One may also
174
 
instantiate an exception first before raising it and add any attributes to it as
175
 
desired. ::
 
171
One may also instantiate an exception first before raising it and add any
 
172
attributes to it as desired. ::
176
173
 
177
174
   >>> try:
178
175
   ...    raise Exception('spam', 'eggs')
221
218
     File "<stdin>", line 1, in ?
222
219
   NameError: HiThere
223
220
 
224
 
The sole argument to :keyword:`raise` indicates the exception to be raised.
225
 
This must be either an exception instance or an exception class (a class that
226
 
derives from :class:`Exception`).
 
221
The argument to :keyword:`raise` is an exception class or instance to be
 
222
raised.  There is a deprecated alternate syntax that separates class and
 
223
constructor arguments; the above could be written as ``raise NameError,
 
224
'HiThere'``.  Since it once was the only one available, the latter form is
 
225
prevalent in older code.
227
226
 
228
227
If you need to determine whether an exception was raised but don't intend to
229
228
handle it, a simpler form of the :keyword:`raise` statement allows you to
246
245
User-defined Exceptions
247
246
=======================
248
247
 
249
 
Programs may name their own exceptions by creating a new exception class.
250
 
Exceptions should typically be derived from the :exc:`Exception` class, either
251
 
directly or indirectly.  For example::
 
248
Programs may name their own exceptions by creating a new exception class (see
 
249
:ref:`tut-classes` for more about Python classes).  Exceptions should typically
 
250
be derived from the :exc:`Exception` class, either directly or indirectly.  For
 
251
example::
252
252
 
253
253
   >>> class MyError(Exception):
254
254
   ...     def __init__(self, value):
286
286
       """Exception raised for errors in the input.
287
287
 
288
288
       Attributes:
289
 
           expression -- input expression in which the error occurred
290
 
           message -- explanation of the error
 
289
           expr -- input expression in which the error occurred
 
290
           msg  -- explanation of the error
291
291
       """
292
292
 
293
 
       def __init__(self, expression, message):
294
 
           self.expression = expression
295
 
           self.message = message
 
293
       def __init__(self, expr, msg):
 
294
           self.expr = expr
 
295
           self.msg = msg
296
296
 
297
297
   class TransitionError(Error):
298
298
       """Raised when an operation attempts a state transition that's not
299
299
       allowed.
300
300
 
301
301
       Attributes:
302
 
           previous -- state at beginning of transition
 
302
           prev -- state at beginning of transition
303
303
           next -- attempted new state
304
 
           message -- explanation of why the specific transition is not allowed
 
304
           msg  -- explanation of why the specific transition is not allowed
305
305
       """
306
306
 
307
 
       def __init__(self, previous, next, message):
308
 
           self.previous = previous
 
307
       def __init__(self, prev, next, msg):
 
308
           self.prev = prev
309
309
           self.next = next
310
 
           self.message = message
 
310
           self.msg = msg
311
311
 
312
312
Most exceptions are defined with names that end in "Error," similar to the
313
313
naming of the standard exceptions.