Multiple Exception Handling in Python

Given a piece of code that can throw any of several different exceptions, and one needs to account for all of the potential exceptions that could be raised without creating duplicate code or long, meandering code passages.

If you can handle different exceptions all using a single block of code, they can be grouped together in a tuple as shown in the code given below :

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

try:
    client_obj.get_url(url)
except (URLError, ValueError, SocketTimeout):
    client_obj.remove_url(url)

chevron_right


The remove_url() method will be called if any of the listed exceptions occurs. If, on the other hand, if one of the exceptions has to be handled differently, then put it into its own except clause as shown in the code given below :

Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

try:
    client_obj.get_url(url)
except (URLError, ValueError):
    client_obj.remove_url(url)
except SocketTimeout:
    client_obj.handle_url_timeout(url)

chevron_right


Many exceptions are grouped into an inheritance hierarchy. For such exceptions, all of the exceptions can be caught by simply specifying a base class. For example, instead of writing code as shown in the code given below –

Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

try:
    f = open(filename)
except (FileNotFoundError, PermissionError):
    ...

chevron_right


Except statement can be re-written as in the code given below. This works because OSError is a base class that’s common to both the FileNotFoundError and PermissionError exceptions.

Code #4 :

filter_none

edit
close

play_arrow

link
brightness_4
code

try:
    f = open(filename)
except OSError:
    ...

chevron_right


Although it’s not specific to handle multiple exceptions per se, it is worth noting that one can get a handle to the thrown exception using them as a keyword as shown in the code given below.

Code #5 :

filter_none

edit
close

play_arrow

link
brightness_4
code

try:
    f = open(filename)
  
except OSError as e:
    if e.errno == errno.ENOENT:
        logger.error('File not found')
    elif e.errno == errno.EACCES:
        logger.error('Permission denied')
    else:
        logger.error('Unexpected error: % d', e.errno)

chevron_right


The e variable holds an instance of the raised OSError. This is useful if the exception has to be invested further, such as processing it based on the value of the additional status code. The except clauses are checked in the order listed and the first match executes.

Code #6 : Create situations where multiple except clauses might match

filter_none

edit
close

play_arrow

link
brightness_4
code

f = open('missing')

chevron_right


Output :

Traceback (most recent call last):
File "", line 1, in 
FileNotFoundError: [Errno 2] No such file or directory: 'miss
filter_none

edit
close

play_arrow

link
brightness_4
code

try:
    f = open('missing')
    except OSError:
        print('It failed')
    except FileNotFoundError:
        print('File not found')

chevron_right


Output :

Failed

Here the except FileNotFoundError clause doesn’t execute because the OSError is more general, matches the FileNotFoundError exception, and was listed first.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.