Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
8a85ac66
Commit
8a85ac66
authored
Mar 19, 2006
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update tutorial wrt PEP 341 try-except-finally statement
parent
c54ae357
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
13 deletions
+43
-13
Doc/tut/tut.tex
Doc/tut/tut.tex
+43
-13
No files found.
Doc/tut/tut.tex
View file @
8a85ac66
...
@@ -3692,19 +3692,49 @@ Traceback (most recent call last):
...
@@ -3692,19 +3692,49 @@ Traceback (most recent call last):
KeyboardInterrupt
KeyboardInterrupt
\end
{
verbatim
}
\end
{
verbatim
}
A
\emph
{
finally clause
}
is executed whether or not an exception has
A
\emph
{
finally clause
}
is always executed before leaving the
occurred in the try clause. When an exception has occurred, it is
\keyword
{
try
}
statement, whether an exception has occurred or not.
re
-
raised after the finally clause is executed. The finally clause is
When an exception has occurred in the
\keyword
{
try
}
clause and has not
also executed ``on the way out'' when the
\keyword
{
try
}
statement is
been handled by an
\keyword
{
except
}
clause
(
or it has occurred in a
left via a
\keyword
{
break
}
or
\keyword
{
return
}
statement.
\keyword
{
except
}
or
\keyword
{
else
}
clause
)
, it is re
-
raised after the
\keyword
{
finally
}
clause has been executed. The
\keyword
{
finally
}
clause
The code in the finally clause is useful for releasing external
is also executed ``on the way out'' when any other clause of the
resources
(
such as files or network connections
)
, regardless of
\keyword
{
try
}
statement is left via a
\keyword
{
break
}
,
\keyword
{
continue
}
whether the use of the resource was successful.
or
\keyword
{
return
}
statement. A more complicated example:
A
\keyword
{
try
}
statement must either have one or more except clauses
\begin
{
verbatim
}
or one finally clause, but not both
(
because it would be unclear which
>>> def divide
(
x, y
)
:
clause should be executed first
)
.
... try:
... result
=
x
/
y
... except ZeroDivisionError:
... print "division by zero
!
"
... else:
... print "result is", result
... finally:
... print "executing finally clause"
...
>>> divide
(
2
,
1
)
result is
2
executing finally clause
>>> divide
(
2
,
0
)
division by zero
!
executing finally clause
>>> divide
(
"
2
", "
1
"
)
executing finally clause
Traceback
(
most recent call last
)
:
File "<stdin>", line
1
, in ?
File "<stdin>", line
3
, in divide
TypeError: unsupported operand type
(
s
)
for
/
: 'str' and 'str'
\end
{
verbatim
}
As you can see, the
\keyword
{
finally
}
clause is executed in any
event. The
\exception
{
TypeError
}
raised by dividing two strings
is not handled by the
\keyword
{
except
}
clause and therefore
re
-
raised after the
\keyword
{
finally
}
clauses has been executed.
In real world applications, the
\keyword
{
finally
}
clause is useful
for releasing external resources
(
such as files or network connections
)
,
regardless of whether the use of the resource was successful.
\chapter
{
Classes
\label
{
classes
}}
\chapter
{
Classes
\label
{
classes
}}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment