File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -169,13 +169,33 @@ \section{Exceptions}
169169When an exception is not handled at all, the interpreter terminates
170170execution of the program, or returns to its interactive main loop.
171171
172- Exceptions are identified by string objects. Two different string
173- objects with the same value identify different exceptions.
172+ Exceptions are identified by string objects or class instances. Two
173+ different string objects with the same value identify different
174+ exceptions. An exception can be raised with a class instance. Such
175+ exceptions are caught by specifying an except clause that has the
176+ class name (or a base class) as the condition.
174177
175178When an exception is raised, an object (maybe \verb @None @) is passed
176179as the exception's `` parameter'' ; this object does not affect the
177180selection of an exception handler, but is passed to the selected
178- exception handler as additional information.
181+ exception handler as additional information. For exceptions raised
182+ with a class instance, the instance is passed as the `` parameter'' .
183+
184+ For example:
185+
186+ \begin {verbatim }
187+ >>> class Error:
188+ ... def __init__(self, msg): self.msg = msg
189+ ...
190+ >>> class SpecificError(Error): pass
191+ ...
192+ >>> try:
193+ ... raise SpecificError('broken')
194+ ... except Error, obj:
195+ ... print obj.msg
196+ ...
197+ broken
198+ \end {verbatim }
179199
180200See also the description of the \verb @try @ and \verb @raise @
181201statements.
Original file line number Diff line number Diff line change @@ -169,13 +169,33 @@ \section{Exceptions}
169169When an exception is not handled at all, the interpreter terminates
170170execution of the program, or returns to its interactive main loop.
171171
172- Exceptions are identified by string objects. Two different string
173- objects with the same value identify different exceptions.
172+ Exceptions are identified by string objects or class instances. Two
173+ different string objects with the same value identify different
174+ exceptions. An exception can be raised with a class instance. Such
175+ exceptions are caught by specifying an except clause that has the
176+ class name (or a base class) as the condition.
174177
175178When an exception is raised, an object (maybe \verb @None @) is passed
176179as the exception's `` parameter'' ; this object does not affect the
177180selection of an exception handler, but is passed to the selected
178- exception handler as additional information.
181+ exception handler as additional information. For exceptions raised
182+ with a class instance, the instance is passed as the `` parameter'' .
183+
184+ For example:
185+
186+ \begin {verbatim }
187+ >>> class Error:
188+ ... def __init__(self, msg): self.msg = msg
189+ ...
190+ >>> class SpecificError(Error): pass
191+ ...
192+ >>> try:
193+ ... raise SpecificError('broken')
194+ ... except Error, obj:
195+ ... print obj.msg
196+ ...
197+ broken
198+ \end {verbatim }
179199
180200See also the description of the \verb @try @ and \verb @raise @
181201statements.
You can’t perform that action at this time.
0 commit comments