0

I think this should be a bit tricky but somehow feasible, but I need help. I'd like to execute two functions from within my main() func. I'd like to be able to catch exceptions from the two separately, but still being able to execute both and get the result of at least one of them if the other raises an exception.

Let's say I have:

def foo():
    raise TypeError

def bar():
    return 'bar'

If I do (adapted from here):

def multiple_exceptions(flist):
    for f in flist:
        try:
            return f()
        except:
            continue

def main():
    multiple_exceptions([foo, bar])

main()

main() would return 'bar', but I'd like to be able to still throw the exception from foo() after all. This way, I would still have the result of one of my functions and the information on the error occurred in the other.

umbe1987
  • 2,894
  • 6
  • 35
  • 63
  • how you are calling from main these methods ? – Jitesh Mohite Nov 20 '17 at 11:23
  • I edited the code to answer this. – umbe1987 Nov 20 '17 at 11:24
  • What output you are getting right now ? – Jitesh Mohite Nov 20 '17 at 11:26
  • Sorry, the code was wrong (I missed the essential for loop). Anyway, as I said, main() return 'bar' so far. I'd like to be able to also catch the TypeError exception (returned by foo()) and raise it afterwards in my main(). Thanks for the help. – umbe1987 Nov 20 '17 at 11:30
  • 2
    Exceptions are just objects and you can do what you want with them after catching it. For example you can store them in a list each time you catch them, along with any additional info you want. Then do what you want with them afterwards - if that's raising them again, that's completely fine, you can do that. Perhaps it's just logging them. Either way, they are just objects and nothing special - you can do whatever you want with them once they have been caught in an `except` block. Syntax to actually catch it is `except type as name:` e.g. `except IOError as myError:` –  Nov 20 '17 at 11:35

2 Answers2

1

You can capture and store the exceptions using 'as', e.g.:

try:
    raise Exception('I am an error!')
    print('The poster messed up error-handling code here.') #should not be displayed
except Exception as Somename:
    print(Somename.message) 
    # you'll see the error message displayed as a normal print result; 
    # you could do print(stuff, file=sys.stderr) to print it like an error without aborting

print('Code here still works, the function did not abort despite the error above')

...or you can do:
except Exception as Somename:
    do_stuff()
    raise Somename
jkm
  • 704
  • 4
  • 7
  • 1
    Edited - I thought that my original answer might seem to imply you have to use print w/ sys.stderr to get the error message; you can also simply reraise the caught error at your leisure later on, with all the consequences of doing so. – jkm Nov 20 '17 at 11:54
0

Thanks for the comments. I solved by doing this:

def multiple_exceptions(flist):

    exceptions = []

    for f in flist:
        try:
            f()
        except Exception as  e:
            exceptions.append(e.message)
            continue

    return exceptions

def main():
    multiple_exceptions([foo, bar])

error_messages = main() # list of e.messages occurred (to be raised whenever I want)

Then I can raise my exception like e.g. raise Exception(error_messages[0]) (I only care about the first in this case let's say).

umbe1987
  • 2,894
  • 6
  • 35
  • 63
  • If you feel this answers the question best; accept it as the answer. You don't have to leave the question open just because you wrote the solution - there is absolutely nothing wrong with accepting your own answer if it's correct. –  Nov 20 '17 at 12:22