raise errors

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Sep 21 08:10:22 EDT 2009


On Mon, Sep 21, 2009 at 5:17 AM, daved170 <daved170 at gmail.com> wrote:
> Hi everybody,
> I need help with exceptions raising.
> My goal is to print at the outer functions all the errors including
> the most inner one.
>
> For example:
>
> def foo1(self):
>   try:
>        foo2()
>   except ? :
>         print "outer Err at foo1" + ??
>
> def foo2(self):
>   try:
>        error occured
>   except ? :
>         raise "inner Err at foo2"
>
>
> the ? remarks that I have no idea what to use.
>
> I would like the print to be : outer Err at foo1 , inner Err at foo1
>
> thanks
> daved
> --

First of all, what version of Python are you using? String exceptions
are deprecated in Python 2.5 and removed in 2.6. You should switch to
using Exceptions. In particular, you'll probably want to subclass
Exception.

String exceptions use the syntax "except 'exception string':" which
means you need to know the string ahead of time. You'll instead want
to do this.

def foo1() :
   try :
     foo2()
   except Exception, e :
      print "outer exception at foo1, %s" % str(e)

def foo2() :
   raise Exception("inner exception at foo2")

In Python 3.x, the syntax changed to "except Exception as e" but that
syntax isn't available in versions prior to 2.6


> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list