473,327 Members | 1,979 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,327 software developers and data experts.

Multiple exception syntax

Normally in Python there is no need to write parenthesis for a tuple :
a,b,c=(1,2,3)
(a,b,c)=1,2,3

are the same, and so are :

for (x,y) in enumerate(aList)
for x,y in enumerate(aList)

But with "except" the behaviour is very different with or without
parenthesis :
- "except x,y" means : if exception x is raised, then y is the instance of
the x class holding information about the exception
- "except (x,y)" means : if one of the exceptions x or y is raised (or both)

So it took me some time to figure out why this code was wrong :

----------------------
1 import ConfigParser
2 conf=ConfigParser.ConfigParser()
3 conf.read("config.ini")
4
5 try:
6 PORT=conf.get("Server","port")
7 except ConfigParser.NoOptionError,ConfigParser.NoSectionE rror:
8 PORT=80
9
10 try:
11 language=conf.get("Translation","language")
12 except ConfigParser.NoOptionError,ConfigParser.NoSectionE rror:
13 language="default"
----------------------

In my config.ini there was a [Server] section but no "port" option, and no
[Translation] section. I had this very puzzling traceback :

Traceback (most recent call last):
File "C:\Pierre\Programmes Python\multipleExceptBug.py", line 11
, in ?
language=conf.get("Translation","language")
File "C:\Python23\lib\ConfigParser.py", line 505, in get
raise NoSectionError(section)
AttributeError: NoOptionError instance has no __call__ method

My bug was in line 7, where ConfigParser.NoSectionError becomes an
instance of the NoOptionError class. With parenthesis on line 7 (and 12)
it works all right

I find this confusing. It would be clearer for me to have :

"except Error1 or Error2 or Error3"

Or have I drunk too much lately ?
Pierre
Jul 18 '05 #1
1 2117
Pierre Quentel wrote:
I find this confusing. It would be clearer for me to have :

"except Error1 or Error2 or Error3"
I see the ambiguity, but I find the above confusing, too, because it looks
like a boolean expression, which it is not. I'd rather change the syntax to
clearly separate the tuple of exception classes from the target:

"except" [expression ["to" target]] ":" suite

"to" is currently a comma. Don't expect that to happen, though.

By the way, I don't recall having seen expression containing exception
*instances*, and only learned that this is possible when I looked up the
try statement in the reference. Is this for string exceptions or has it an
application in newer code?
Or have I drunk too much lately ?


Obviously, if you have to ask :-)

Peter

Jul 18 '05 #2

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
3
by: Nathan Sokalski | last post by:
When I view any page in my application a second time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize) +313...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
1
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
3
by: Zach | last post by:
I'm sure this comes up often, but I have a situation where I have at least 4 objects that all implement IDisposable. It gets very tedious having to write finally blocks that Dispose all of them in...
4
by: Chris Allen | last post by:
I am confused on one aspect of exception handling. If you specify the exception object type to match in an except statement it is possible to also obtain the exception object itself, but I can't...
2
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). --...
0
by: Nick Keighley | last post by:
Hi, I have, unsuccessfully, checked the net and Stroustrup before I posted here. Is there a way to catch multiple exception types in a single catch statement? Something like: try
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.