on-internet
← Community

Share Python code when handling multiple exceptions

Stack Overflow ·

I've written a program that needs to deal with a function that can throw multiple exceptions. For each exception I catch I have some code that will handle it specifically. However, I also have some code I want to run no matter which exception was caught. My current solution is a handle_exception() function which is called from each except block. try: throw_multiple_exceptions() except FirstException as excep: handle_first_exception() handle_exception() except SecondException as excep: handle_second_exception() handle_exception() Is there a better way to do this? I would like the code to look l

Comments

Sign in or sign up to comment and upvote.

No comments yet.