[Python-checkins] CVS: python/dist/src/Doc/tut tut.tex,1.149,1.150
Fred L. Drake
fdrake@users.sourceforge.net
Fri, 21 Sep 2001 14:10:07 -0700
Update of /cvsroot/python/python/dist/src/Doc/tut
In directory usw-pr-cvs1:/tmp/cvs-serv18697/tut
Modified Files:
tut.tex
Log Message:
Exceptions in interactive examlpes did not always include the indication of
the source file using "in ?".
Added a description of the bare "raise" statement.
Added more description and examples for user-defined exceptions; this
is part of a response to SF bug #443559.
Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.149
retrieving revision 1.150
diff -C2 -d -r1.149 -r1.150
*** tut.tex 2001/09/06 18:41:15 1.149
--- tut.tex 2001/09/21 21:10:05 1.150
***************
*** 620,624 ****
'string'
>>> string.strip('str') 'ing' # <- This is invalid
! File "<stdin>", line 1
string.strip('str') 'ing'
^
--- 620,624 ----
'string'
>>> string.strip('str') 'ing' # <- This is invalid
! File "<stdin>", line 1, in ?
string.strip('str') 'ing'
^
***************
*** 729,733 ****
>>> word[-10] # error
Traceback (most recent call last):
! File "<stdin>", line 1
IndexError: string index out of range
\end{verbatim}
--- 729,733 ----
>>> word[-10] # error
Traceback (most recent call last):
! File "<stdin>", line 1, in ?
IndexError: string index out of range
\end{verbatim}
***************
*** 1835,1839 ****
[[2, 4], [4, 16], [6, 36]]
>>> [x, x**2 for x in vec] # error - parens required for tuples
! File "<stdin>", line 1
[x, x**2 for x in vec]
^
--- 1835,1839 ----
[[2, 4], [4, 16], [6, 36]]
>>> [x, x**2 for x in vec] # error - parens required for tuples
! File "<stdin>", line 1, in ?
[x, x**2 for x in vec]
^
***************
*** 2962,2966 ****
\begin{verbatim}
>>> while 1 print 'Hello world'
! File "<stdin>", line 1
while 1 print 'Hello world'
^
--- 2962,2966 ----
\begin{verbatim}
>>> while 1 print 'Hello world'
! File "<stdin>", line 1, in ?
while 1 print 'Hello world'
^
***************
*** 2988,3000 ****
>>> 10 * (1/0)
Traceback (most recent call last):
! File "<stdin>", line 1
ZeroDivisionError: integer division or modulo
>>> 4 + spam*3
Traceback (most recent call last):
! File "<stdin>", line 1
NameError: spam
>>> '2' + 2
Traceback (most recent call last):
! File "<stdin>", line 1
TypeError: illegal argument type for built-in operation
\end{verbatim}
--- 2988,3000 ----
>>> 10 * (1/0)
Traceback (most recent call last):
! File "<stdin>", line 1, in ?
ZeroDivisionError: integer division or modulo
>>> 4 + spam*3
Traceback (most recent call last):
! File "<stdin>", line 1, in ?
NameError: spam
>>> '2' + 2
Traceback (most recent call last):
! File "<stdin>", line 1, in ?
TypeError: illegal argument type for built-in operation
\end{verbatim}
***************
*** 3171,3175 ****
>>> raise NameError, 'HiThere'
Traceback (most recent call last):
! File "<stdin>", line 1
NameError: HiThere
\end{verbatim}
--- 3171,3175 ----
>>> raise NameError, 'HiThere'
Traceback (most recent call last):
! File "<stdin>", line 1, in ?
NameError: HiThere
\end{verbatim}
***************
*** 3179,3190 ****
argument.
\section{User-defined Exceptions \label{userExceptions}}
! Programs may name their own exceptions by assigning a string to a
! variable or creating a new exception class. For example:
\begin{verbatim}
! >>> class MyError:
... def __init__(self, value):
... self.value = value
--- 3179,3209 ----
argument.
+ If you need to determine whether an exception was raised but don't
+ intend to handle it, a simpler form of the \keyword{raise} statement
+ allows you to re-raise the exception:
+
+ \begin{verbatim}
+ >>> try:
+ ... raise NameError, 'HiThere'
+ ... except NameError:
+ ... print 'An exception flew by!'
+ ... raise
+ ...
+ An exception flew by!
+ Traceback (most recent call last):
+ File "<stdin>", line 2, in ?
+ NameError: HiThere
+ \end{verbatim}
+
\section{User-defined Exceptions \label{userExceptions}}
! Programs may name their own exceptions by creating a new exception
! class. Exceptions should typically be derived from the
! \exception{Exception} class, either directly or indirectly. For
! example:
\begin{verbatim}
! >>> class MyError(Exception):
... def __init__(self, value):
... self.value = value
***************
*** 3198,3214 ****
...
My exception occurred, value: 4
! >>> raise MyError, 1
Traceback (most recent call last):
! File "<stdin>", line 1
! __main__.MyError: 1
\end{verbatim}
! Many standard modules use this to report errors that may occur in
! functions they define.
! More information on classes is presented in chapter \ref{classes},
! ``Classes.''
\section{Defining Clean-up Actions \label{cleanup}}
--- 3217,3275 ----
...
My exception occurred, value: 4
! >>> raise MyError, 'oops!'
Traceback (most recent call last):
! File "<stdin>", line 1, in ?
! __main__.MyError: 'oops!'
\end{verbatim}
! Exception classes can be defined which do anything any other class can
! do, but are usually kept simple, often only offering a number of
! attributes that allow information about the error to be extracted by
! handlers for the exception. When creating a module which can raise
! several distinct errors, a common practice is to create a base class
! for exceptions defined by that module, and subclass that to create
! specific exception classes for different error conditions:
! \begin{verbatim}
! class Error(Exception):
! """Base class for exceptions in this module."""
! pass
!
! class InputError(Error):
! """Exception raised for errors in the input.
!
! Attributes:
! expression -- input expression in which the error occurred
! message -- explanation of the error
! """
!
! def __init__(self, expression, message):
! self.expression = expression
! self.message = message
!
! class TransitionError(Error):
! """Raised when an operation attempts a state transition that's not
! allowed.
+ Attributes:
+ previous -- state at beginning of transition
+ next -- attempted new state
+ message -- explanation of why the specific transition is not allowed
+ """
+
+ def __init__(self, previous, next, message):
+ self.previous = previous
+ self.next = next
+ self.message = message
+ \end{verbatim}
+
+ Most exceptions are defined with names that end in ``Error,'' similar
+ to the naming of the standard exceptions.
+ Many standard modules define their own exceptions to report errors
+ that may occur in functions they define. More information on classes
+ is presented in chapter \ref{classes}, ``Classes.''
+
+
\section{Defining Clean-up Actions \label{cleanup}}
***************
*** 3225,3229 ****
Goodbye, world!
Traceback (most recent call last):
! File "<stdin>", line 2
KeyboardInterrupt
\end{verbatim}
--- 3286,3290 ----
Goodbye, world!
Traceback (most recent call last):
! File "<stdin>", line 2, in ?
KeyboardInterrupt
\end{verbatim}
***************
*** 3235,3240 ****
--- 3296,3306 ----
left via a \keyword{break} or \keyword{return} statement.
+ The code in the finally clause is useful for releasing external
+ resources (such as files or network connections), regardless of
+ whether or not the use of the resource was successful.
+
A \keyword{try} statement must either have one or more except clauses
or one finally clause, but not both.
+
\chapter{Classes \label{classes}}