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

How can I obtain the exception object on a generlized except statement?

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 figure out how
to get the exception object when I don't specify a match.

for example:
>>try: urlopen('http://www.google.com')
except socket.error, msg:
print str(msg)
this works and I can do what I like with the exception object (msg).
but I can't do this with a simple except statment.
>>except msg:
this won't work because it will think msg is the type to match
>>except ,msg:
syntax error
>>except *,msg:
syntax error
>>except (),msg:
Hmm I didn't try that one before I started this post. It doesn't give
me a syntax error. I'll experiment.
Even if the above syntax works, is this the way to do this? It seems
sort of funky.

How do I do this? Thanks.

Jun 10 '07 #1
4 1223
Chris Allen schrieb:
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 figure out how
to get the exception object when I don't specify a match.

for example:
>>>try: urlopen('http://www.google.com')
except socket.error, msg:
print str(msg)

this works and I can do what I like with the exception object (msg).
but I can't do this with a simple except statment.
>>>except msg:

this won't work because it will think msg is the type to match
>>>except ,msg:

syntax error
>>>except *,msg:

syntax error
>>>except (),msg:

Hmm I didn't try that one before I started this post. It doesn't give
me a syntax error. I'll experiment.
Even if the above syntax works, is this the way to do this? It seems
sort of funky.

How do I do this? Thanks.
pydoc sys.exc_info
Diez
Jun 10 '07 #2
On Jun 10, 10:13 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:
Chris Allen schrieb:
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 figure out how
to get the exception object when I don't specify a match.
for example:
>>try: urlopen('http://www.google.com')
except socket.error, msg:
print str(msg)
this works and I can do what I like with the exception object (msg).
but I can't do this with a simple except statment.
>>except msg:
this won't work because it will think msg is the type to match
>>except ,msg:
syntax error
>>except *,msg:
syntax error
>>except (),msg:
Hmm I didn't try that one before I started this post. It doesn't give
me a syntax error. I'll experiment.
Even if the above syntax works, is this the way to do this? It seems
sort of funky.
How do I do this? Thanks.

pydoc sys.exc_info
In particular, you want sys.exc_info()[:2]


Jun 10 '07 #3
Just what I was looking for thanks Diez and John.

Jun 10 '07 #4
Chris Allen <ca******@gmail.comwrote:
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 figure out how
to get the exception object when I don't specify a match.
In most cases you can just catch Exception. If you want to be sure to
catch deprecated string exceptions also then use sys.exc_info().

try:
... something ...
except Exception, e:
print e

Also what you want to do with it when you've caught it makes a
difference:

If you are planning on logging a stack backtrace then you'll want the
traceback as well as the exception, so sys.exc_info() might be indicated
except that in that case you'll be better off using logging.exception()
instead.

try:
... something ...
except:
logging.exception("Unexpected error")

If you are just planning on swallowing the exception then you don't want
any of these: you want to catch the specific exceptions that you expect
to be raised.
>except (),msg:
Using an empty tuple for the exception specification won't catch any
exceptions. Not very useful if the tuple is a literal, but it could be
useful in some obscure situations (e.g. an except clause to handle
exceptions declared in a specific DLL which might not always be
present).
Jun 11 '07 #5

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

Similar topics

6
by: RT Lange | last post by:
>>> class E1(Exception): pass >>> class E2(E1): pass >>> i = E2('foo') >>> raise E1(i) Traceback (most recent call last): File "<pyshell#5>", line 1, in ? raise E1(i) E1: foo
8
by: StepH | last post by:
Hi, I've this module : from optparse import OptionParser import re _version = "P1" def writeVar(f, line):
13
by: Aggelos I. Orfanakos | last post by:
Hello. In a program, I want to ensure that a socket closes (so I use try ... finally), but I also want to catch/handle a socket exception. This is what I have done: try: try: s = ... #...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
2
by: William Payne | last post by:
Hello. Consider the following code snippet (pseudo-code). void foo() { /* do stuff */ if fatal error occurs { throw "error message"; }
2
by: pegazik | last post by:
Hello. I have problem and I ask you for help. Probably there is some quite easy solution, but I can't see it. I'm trying to perform some action that have to be timeout safe. So here is the...
6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
3
by: Martin | last post by:
Hi. I need to identify the type of the exception in the universal handler (catch (...)) for debugging purposes. Point is that I write a testing console application, which must call some...
2
by: crybaby | last post by:
Why you specify type and name of the exception in your custom exceptions, but not in built in exceptions except IOError: print "no file by the name ccy_rates*.txt" except MyError, e: print...
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.