exceptions?

zsoltman at my-deja.com zsoltman at my-deja.com
Thu Dec 16 16:43:22 EST 1999


Mike,
I noticed someone pointed you to some good documentation.
The most basic is
try:
  do_something()
catch
  do_something_else()

The problem is, what if you don't want your program to stop, but you
would like to capture the stack trace for future analysis.

I just recently figured out how to do this, so I will share.  Keep in
mind, there are probably more eloquent ways of handling this.

sys.exc_info()[2] is the traceback object that you can
capture at error time (or any time you want)
Printing it out is a little screwy, but not that difficult.
______________________________________________________________
Example:
def HandleTraceBack(excinfo2,msg):
    import traceback
    tb=traceback.format_list(traceback.extract_tb(excinfo2))
    for each in tb:
        msg=msg + each
    raise ValueError,msg

####In the main program (or other functions) use
import sys
try:
    do_function()
except:
    HandleTraceBack(sys.exc_info()[2],
                    '****ERROR attempting do_function\n')
___________________________________________________________

In article <8388m7$1ii$1 at nnrp1.deja.com>,
  moonseeker at my-deja.com wrote:
> Hi!
>
> Could anyone please point me to a good explanation how exceptions can
be
> handled, new created and used, how I can check for a special
exception and so
> on...?
>
> Thanks,
> Mike
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list