473,394 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

about try statement

very hard for me to understand the difference between
try...except and try...finally
Nov 22 '05 #1
2 1484
Shi Mu enlightened us with:
very hard for me to understand the difference between try...except
and try...finally


Within a 'try' block, if an exception is called and a matching
'except' block is found, that block will be used to handle the
expression.

From the documentation of the "return" keyword: "When return passes
control out of a try statement with a finally clause, that finally
clause is executed before really leaving the function." Note that
there is no talk about exceptions in this.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Nov 22 '05 #2
On Sat, 12 Nov 2005 10:20:08 +0100, Sybren Stuvel <sy*******@YOURthirdtower.com.imagination> wrote:
Shi Mu enlightened us with:
very hard for me to understand the difference between try...except
and try...finally


Within a 'try' block, if an exception is called and a matching
'except' block is found, that block will be used to handle the
expression.

From the documentation of the "return" keyword: "When return passes
control out of a try statement with a finally clause, that finally
clause is executed before really leaving the function." Note that
there is no talk about exceptions in this.

Which is because in the finally block the exception is still "live"
and will take effect as soon as the finally block is left, e.g.,
try: ... try: 1/0
... finally: print 'exception can not bypass finally'
... except Exception, e:
... print '%s: %s' %(e.__class__.__name__, e)
...
exception can not bypass finally
ZeroDivisionError: integer division or modulo by zero

Or to make the point about return def foo(den): ... try:
... try: return 1/den
... finally: print 'exception can not bypass finally'
... except Exception, e:
... print '%s: %s' %(e.__class__.__name__, e)
... return 'succeeded in returning something'
... foo(0) exception can not bypass finally
ZeroDivisionError: integer division or modulo by zero
'succeeded in returning something' foo(1)

exception can not bypass finally
1

Regards,
Bengt Richter
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: Dirk Försterling | last post by:
Hi, sorry for reposting, but it seems my message just hit nothing but air. If I posted to the wrong list or did something else wrong with the message, please let me know. I really want to...
15
by: Thelma Lubkin | last post by:
formA determines some_where and some_value and issues docmd.close ,Me docmd.openform "formB", , ,some_where, , ,some_value formB receives the correct some_where and some_value After...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
9
by: Michael | last post by:
Hi all, I would like to get people's opinion about executing SQL statements in C# (or any other .NET language really). I used to create my SQL statement by building a string and replacing single...
11
by: Sensei | last post by:
Hi again! I have still curiosity about the reason of some C constructs/keywords... The first is about static functions. What was the reason of restricting a function to be visible just in a...
4
by: kernelxu | last post by:
Hi,folks. I got some suggestion about bitwise shift from <The C Book, second edition>(written by Mike Banahan, Declan Brady and Mark Doran, originally published by Addison Wesley in 1991. This...
8
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
by: Caper | last post by:
Hello everyone, I am a bit confused about Innodb deadlock and the "SELECT…FOR UPDATE” statement. 1. Here is a deadlock example 1) Create actor table CREATE TABLE actor(actor_id INT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.