Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use match case to handle exceptions

I have a method that receives an exception of type Exception as a parameter and I want to filter which exceptions to show to the user and which to show as general errors.

Since I have more than 10 of these exceptions in the real code, I wanted to use a match case that filters out the different cases.

I only want to match the type of exceptions and I don’t want to use an if elif for each exception if possible, as code inside each if would be the same.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def handle_exception(self, exception: Exception):
    # Built the error message, based on which exception was caught
    match exception:
        case MyCustomExceptionOne() \
             | MyCustomExceptionTwo() \
             | MyCustomExceptionThree():
            show_error_message(exception) 
        case _:
            show_error_message("Error occured") 

Am I missing something?

>Solution :

There is no need nor advantage for case match just to match multiple types: An if can efficiently match on multiple types using if isinstance(obj, (type1, type2, ...)).

def handle_exception(self, exception: Exception):
    # Built the error message, based on which exception was caught
    if isinstance(
        exception,
        (MyCustomExceptionOne, MyCustomExceptionTwo, MyCustomExceptionThree)
    ):
        show_error_message(exception) 
    else:
        show_error_message("Error occured") 

This is equivalent to what except (type1, type2, ...): does under the hood, thus matching the usual exception handling semantics.

Note that the second argument to isinstance is a regular tuple here. If code clarity is an issue, it can be defined elsewhere and simply referenced:

MY_EXCEPTIONS = (MyCustomExceptionOne, MyCustomExceptionTwo, MyCustomExceptionThree)

def handle_exception(self, exception: Exception):
    # Built the error message, based on which exception was caught
    if isinstance(exception, MY_EXCEPTIONS):
        show_error_message(exception) 
    else:
        show_error_message("Error occured") 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading