Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb6fd5d

Browse files
committed
#10856: document (Base)Exception.args better.
1 parent 7603fa0 commit fb6fd5d

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

Doc/library/exceptions.rst

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ equivalent, even if they have the same name.
1818

1919
The built-in exceptions listed below can be generated by the interpreter or
2020
built-in functions. Except where mentioned, they have an "associated value"
21-
indicating the detailed cause of the error. This may be a string or a tuple
22-
containing several items of information (e.g., an error code and a string
23-
explaining the code). The associated value is usually passed to the exception
24-
class's constructor. If the exception class is derived from the standard root
25-
class :exc:`BaseException`, the associated value is present as the exception
26-
instance's :attr:`args` attribute.
21+
indicating the detailed cause of the error. This may be a string or a tuple of
22+
several items of information (e.g., an error code and a string explaining the
23+
code). The associated value is usually passed as arguments to the exception
24+
class's constructor.
2725

2826
User code can raise built-in exceptions. This can be used to test an exception
2927
handler or to report an error condition "just like" the situation in which the
@@ -38,16 +36,32 @@ defining exceptions is available in the Python Tutorial under
3836

3937
The following exceptions are used mostly as base classes for other exceptions.
4038

41-
.. XXX document with_traceback()
42-
4339
.. exception:: BaseException
4440

4541
The base class for all built-in exceptions. It is not meant to be directly
46-
inherited by user-defined classes (for that use :exc:`Exception`). If
42+
inherited by user-defined classes (for that, use :exc:`Exception`). If
4743
:func:`bytes` or :func:`str` is called on an instance of this class, the
48-
representation of the argument(s) to the instance are returned or the empty
49-
string when there were no arguments. All arguments are stored in :attr:`args`
50-
as a tuple.
44+
representation of the argument(s) to the instance are returned, or the empty
45+
string when there were no arguments.
46+
47+
.. attribute:: args
48+
49+
The tuple of arguments given to the exception constructor. Some built-in
50+
exceptions (like :exc:`IOError`) expect a certain number of arguments and
51+
assign a special meaning to the elements of this tuple, while others are
52+
usually called only with a single string giving an error message.
53+
54+
.. method:: with_traceback(tb)
55+
56+
This method sets *tb* as the new traceback for the exception and returns
57+
the exception object. It is usually used in exception handling code like
58+
this::
59+
60+
try:
61+
...
62+
except SomeException:
63+
tb = sys.exc_info()[2]
64+
raise OtherException(...).with_traceback(tb)
5165

5266

5367
.. exception:: Exception

0 commit comments

Comments
 (0)