473,387 Members | 2,436 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,387 software developers and data experts.

catching all exceptions

Hello,

I'd like to catch all exeptions and be able to inspect them.

The simple case: I know which exceptions I'll get:

# standard textbook example:
try:
something()
except ThisException, e:
print "some error occurred: ", str(e)
The not-so-simple case: Handling all other exceptions:

# nice-to-have:
try:
something()
except *, e:
print "some error occurred: ", type(e), str(e)
Well, actually the second statement doesn't even compile... any ideas
why I shouldn't be able to catch "anonymous" exceptions like this, or
whether and how I can (and only overlooked it)?
TIA!
Kind Regards,
Toni
Aug 13 '05 #1
4 1787
a_****@web.de wrote:
Hello,

I'd like to catch all exeptions and be able to inspect them.

The simple case: I know which exceptions I'll get:

# standard textbook example:
try:
something()
except ThisException, e:
print "some error occurred: ", str(e)
The not-so-simple case: Handling all other exceptions:

# nice-to-have:
try:
something()
except *, e:
print "some error occurred: ", type(e), str(e)

except Exception:# catch them all.

Then use moudule 'traceback' to inspect

Paolino

___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
Aug 13 '05 #2
On Sat, 13 Aug 2005 17:42:00 +0200, a_****@web.de wrote:
Hello,

I'd like to catch all exeptions and be able to inspect them.

The simple case: I know which exceptions I'll get:

# standard textbook example:
try:
something()
except ThisException, e:
print "some error occurred: ", str(e)
The not-so-simple case: Handling all other exceptions:

# nice-to-have:
try:
something()
except *, e:
print "some error occurred: ", type(e), str(e)
Well, actually the second statement doesn't even compile... any ideas
why I shouldn't be able to catch "anonymous" exceptions like this, or
whether and how I can (and only overlooked it)?
TIA!

def test(something): ... try:
... something()
... except Exception, e:
... print '%s: %s'% (e.__class__.__name__, e)
... test(lambda: 1/0) ZeroDivisionError: integer division or modulo by zero test(lambda: unk) NameError: global name 'unk' is not defined test(lambda: open('unk')) IOError: [Errno 2] No such file or directory: 'unk'

All exceptions should derive from Exception, so the above should catch them,
although there are deprecated string exceptions that will not be caught.
You can catch these with a bare except, but it is probably better not to,
so you will know there's something to clean up.

If you do have to deal with them, you can then catch them by name individually,
or all with the bare except, e.g.,
def strexraiser(s): raise s ... def test(something): ... try:
... something()
... except Exception, e:
... print '%s: %s'% (e.__class__.__name__, e)
... except 'ugh':
... print 'Caught "ugh"'
... except:
... print sys.exc_info()
... import sys # forgot, will need when above executes ;-)

test(lambda:strexraiser('ugh')) Caught "ugh" test(lambda:strexraiser('gak')) ('gak', None, <traceback object at 0x02EF0ACC>) test(lambda:1/0)

ZeroDivisionError: integer division or modulo by zero

Regards,
Bengt Richter
Aug 13 '05 #3
> Hello,

I'd like to catch all exeptions and be able to inspect them.

The simple case: I know which exceptions I'll get:

# standard textbook example:
try:
something()
except ThisException, e:
print "some error occurred: ", str(e)
The not-so-simple case: Handling all other exceptions:

# nice-to-have:
try:
something()
except *, e:
print "some error occurred: ", type(e), str(e)
Well, actually the second statement doesn't even compile... any ideas
why I shouldn't be able to catch "anonymous" exceptions like this, or
whether and how I can (and only overlooked it)?
TIA!
Kind Regards,
Toni


Try this:

import sys

try:
something()
except:
info = sys.exc_info()
...

and you can inspect the tuple info, which contains the exception type,
value, and traceback.

Best regards,
Tom
Aug 13 '05 #4
Hello,

Tomasz Lisowski wrote:
Well, actually the second statement doesn't even compile... any ideas
why I shouldn't be able to catch "anonymous" exceptions like this, or
whether and how I can (and only overlooked it)?

[ one of three suggestions: ]
Try this:


Ok, so the answer simply was that I didn't see "it", although the
solution is in the manual.

Thank you!
Kind Regards,
Toni
Aug 13 '05 #5

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

Similar topics

1
by: Rolf | last post by:
I understand a compilation error occurs when a method that throws no exceptions is the only code in a try block. What I don't understnad is why I can specify the catching of an Exception for a...
2
by: Keith Bolton | last post by:
I am handling exceptions currently using try, except. Generally I don't handle specific exceptions and am catching all. Then if an exception occurs, I would like to capture that error string....
15
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
8
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
7
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
5
by: Simon Tamman | last post by:
I have an object named DisasterRecovery. The Ctor of this object is this: private DisasterRecovery() { Application.ThreadException+= new...
12
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.