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

Skip to content

Commit 13af428

Browse files
committed
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.
1 parent cf69193 commit 13af428

1 file changed

Lines changed: 85 additions & 19 deletions

File tree

Doc/tut/tut.tex

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ \subsection{Strings \label{strings}}
619619
>>> string.strip('str') + 'ing' # <- This is ok
620620
'string'
621621
>>> string.strip('str') 'ing' # <- This is invalid
622-
File "<stdin>", line 1
622+
File "<stdin>", line 1, in ?
623623
string.strip('str') 'ing'
624624
^
625625
SyntaxError: invalid syntax
@@ -728,7 +728,7 @@ \subsection{Strings \label{strings}}
728728
'HelpA'
729729
>>> word[-10] # error
730730
Traceback (most recent call last):
731-
File "<stdin>", line 1
731+
File "<stdin>", line 1, in ?
732732
IndexError: string index out of range
733733
\end{verbatim}
734734

@@ -1834,7 +1834,7 @@ \subsection{List Comprehensions}
18341834
>>> [[x,x**2] for x in vec]
18351835
[[2, 4], [4, 16], [6, 36]]
18361836
>>> [x, x**2 for x in vec] # error - parens required for tuples
1837-
File "<stdin>", line 1
1837+
File "<stdin>", line 1, in ?
18381838
[x, x**2 for x in vec]
18391839
^
18401840
SyntaxError: invalid syntax
@@ -2961,7 +2961,7 @@ \section{Syntax Errors \label{syntaxErrors}}
29612961
29622962
\begin{verbatim}
29632963
>>> while 1 print 'Hello world'
2964-
File "<stdin>", line 1
2964+
File "<stdin>", line 1, in ?
29652965
while 1 print 'Hello world'
29662966
^
29672967
SyntaxError: invalid syntax
@@ -2987,15 +2987,15 @@ \section{Exceptions \label{exceptions}}
29872987
\begin{verbatim}
29882988
>>> 10 * (1/0)
29892989
Traceback (most recent call last):
2990-
File "<stdin>", line 1
2990+
File "<stdin>", line 1, in ?
29912991
ZeroDivisionError: integer division or modulo
29922992
>>> 4 + spam*3
29932993
Traceback (most recent call last):
2994-
File "<stdin>", line 1
2994+
File "<stdin>", line 1, in ?
29952995
NameError: spam
29962996
>>> '2' + 2
29972997
Traceback (most recent call last):
2998-
File "<stdin>", line 1
2998+
File "<stdin>", line 1, in ?
29992999
TypeError: illegal argument type for built-in operation
30003000
\end{verbatim}
30013001
@@ -3170,22 +3170,41 @@ \section{Raising Exceptions \label{raising}}
31703170
\begin{verbatim}
31713171
>>> raise NameError, 'HiThere'
31723172
Traceback (most recent call last):
3173-
File "<stdin>", line 1
3173+
File "<stdin>", line 1, in ?
31743174
NameError: HiThere
31753175
\end{verbatim}
31763176
31773177
The first argument to \keyword{raise} names the exception to be
31783178
raised. The optional second argument specifies the exception's
31793179
argument.
31803180
3181+
If you need to determine whether an exception was raised but don't
3182+
intend to handle it, a simpler form of the \keyword{raise} statement
3183+
allows you to re-raise the exception:
3184+
3185+
\begin{verbatim}
3186+
>>> try:
3187+
... raise NameError, 'HiThere'
3188+
... except NameError:
3189+
... print 'An exception flew by!'
3190+
... raise
3191+
...
3192+
An exception flew by!
3193+
Traceback (most recent call last):
3194+
File "<stdin>", line 2, in ?
3195+
NameError: HiThere
3196+
\end{verbatim}
3197+
31813198
31823199
\section{User-defined Exceptions \label{userExceptions}}
31833200
3184-
Programs may name their own exceptions by assigning a string to a
3185-
variable or creating a new exception class. For example:
3201+
Programs may name their own exceptions by creating a new exception
3202+
class. Exceptions should typically be derived from the
3203+
\exception{Exception} class, either directly or indirectly. For
3204+
example:
31863205
31873206
\begin{verbatim}
3188-
>>> class MyError:
3207+
>>> class MyError(Exception):
31893208
... def __init__(self, value):
31903209
... self.value = value
31913210
... def __str__(self):
@@ -3197,17 +3216,59 @@ \section{User-defined Exceptions \label{userExceptions}}
31973216
... print 'My exception occurred, value:', e.value
31983217
...
31993218
My exception occurred, value: 4
3200-
>>> raise MyError, 1
3219+
>>> raise MyError, 'oops!'
32013220
Traceback (most recent call last):
3202-
File "<stdin>", line 1
3203-
__main__.MyError: 1
3221+
File "<stdin>", line 1, in ?
3222+
__main__.MyError: 'oops!'
32043223
\end{verbatim}
32053224
3206-
Many standard modules use this to report errors that may occur in
3207-
functions they define.
3225+
Exception classes can be defined which do anything any other class can
3226+
do, but are usually kept simple, often only offering a number of
3227+
attributes that allow information about the error to be extracted by
3228+
handlers for the exception. When creating a module which can raise
3229+
several distinct errors, a common practice is to create a base class
3230+
for exceptions defined by that module, and subclass that to create
3231+
specific exception classes for different error conditions:
3232+
3233+
\begin{verbatim}
3234+
class Error(Exception):
3235+
"""Base class for exceptions in this module."""
3236+
pass
3237+
3238+
class InputError(Error):
3239+
"""Exception raised for errors in the input.
3240+
3241+
Attributes:
3242+
expression -- input expression in which the error occurred
3243+
message -- explanation of the error
3244+
"""
3245+
3246+
def __init__(self, expression, message):
3247+
self.expression = expression
3248+
self.message = message
32083249
3209-
More information on classes is presented in chapter \ref{classes},
3210-
``Classes.''
3250+
class TransitionError(Error):
3251+
"""Raised when an operation attempts a state transition that's not
3252+
allowed.
3253+
3254+
Attributes:
3255+
previous -- state at beginning of transition
3256+
next -- attempted new state
3257+
message -- explanation of why the specific transition is not allowed
3258+
"""
3259+
3260+
def __init__(self, previous, next, message):
3261+
self.previous = previous
3262+
self.next = next
3263+
self.message = message
3264+
\end{verbatim}
3265+
3266+
Most exceptions are defined with names that end in ``Error,'' similar
3267+
to the naming of the standard exceptions.
3268+
3269+
Many standard modules define their own exceptions to report errors
3270+
that may occur in functions they define. More information on classes
3271+
is presented in chapter \ref{classes}, ``Classes.''
32113272
32123273
32133274
\section{Defining Clean-up Actions \label{cleanup}}
@@ -3224,7 +3285,7 @@ \section{Defining Clean-up Actions \label{cleanup}}
32243285
...
32253286
Goodbye, world!
32263287
Traceback (most recent call last):
3227-
File "<stdin>", line 2
3288+
File "<stdin>", line 2, in ?
32283289
KeyboardInterrupt
32293290
\end{verbatim}
32303291
@@ -3234,9 +3295,14 @@ \section{Defining Clean-up Actions \label{cleanup}}
32343295
also executed ``on the way out'' when the \keyword{try} statement is
32353296
left via a \keyword{break} or \keyword{return} statement.
32363297
3298+
The code in the finally clause is useful for releasing external
3299+
resources (such as files or network connections), regardless of
3300+
whether or not the use of the resource was successful.
3301+
32373302
A \keyword{try} statement must either have one or more except clauses
32383303
or one finally clause, but not both.
32393304
3305+
32403306
\chapter{Classes \label{classes}}
32413307
32423308
Python's class mechanism adds classes to the language with a minimum

0 commit comments

Comments
 (0)