Python IMDbPY – Error Handling

In this article we will see how we can handle errors related to IMDb module of Python, error like invalid search or data base error network issues that are related to IMDbPY can be caught by checking for the imdb.IMDbErrorexception

In order to handle error we have to import the following

from imdb import IMDbError

Syntax :

try :

    # code

except IMDbError as e:

    # action to handle it

If any error related to IMDb occur then it will get caught by except block.

Below is the implementation.



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing libraries
from imdb import IMDb, IMDbError
  
# try block
try:
      
    # creating instance of imdb
    ia = IMDb()
      
    # getting person (it accept people id only)
    people = ia.get_person('abcd')
      
# except block   
except IMDbError as e:
      
    # priting the exception
    print(e)

chevron_right


Output :

invalid personID "abcd": invalid literal for int() with base 10: 'abcd'

Another example: In this we have turn off the internet connection

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing libraries
from imdb import IMDb, IMDbError
  
# try block
try:
      
    # creating instance of imdb
    ia = IMDb()
      
    # searching person
    people = ia.search_person('abcd')
      
# except block   
except IMDbError as e:
      
    # priting the exception
    print(e)

chevron_right


Output :

{'errcode': None, 'errmsg': 'None', 'url': 'https://www.imdb.com/find?q=abcd&s=nm', 'proxy': '', 'exception type': 'IOError', 'original exception': URLError(gaierror(11001, 'getaddrinfo failed'))}



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.