Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share Python code when handling multiple exceptions

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 like this:

try:
    throw_multiple_exceptions()
except FirstException as excep:
    handle_first_exception()
except SecondException as excep:
    handle_second_exception()
except Exception as excep:
    handle_exception()
like image 519
user4624535 Avatar asked Jan 14 '16 18:01

user4624535


People also ask

How do you handle multiple exceptions in Python?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

Can you have multiple Except statements Python?

🔹 Multiple Except Clauses According to the Python Documentation: A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. In this example, we have two except clauses.

How do you handle different exceptions?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

Can one block of except statements handle multiple expectations?

5. Can one block of except statements handle multiple exception? Answer: a Explanation: Each type of exception can be specified directly. There is no need to put it in a list.


2 Answers

how about PEP 0443? its awesome, and very scalable because all you have to do is code and register new handlers

from functools import singledispatch
@singledispatch
def handle_specific_exception(e): # got an exception we don't handle
    pass       

@handle_specific_exception.register(Exception1)
def _(e):
    # handle exception 1

@handle_specific_exception.register(Exception2)
def _(e):
    # handle exception 2

try:
    throw_multiple_exceptions()
except Exception as e:
    handle_specific_exception(e)
    handle_exception()
like image 102
rbp Avatar answered Oct 21 '22 01:10

rbp


You could do something like:

try:
    throw_multiple_exceptions()
except FirstException, SecondException as excep:
    if isinstance(excep, FirstException):
        handle_first_exception()
    else:
        handle_second_exception()
    handle_exception()
like image 21
jonrsharpe Avatar answered Oct 21 '22 01:10

jonrsharpe