Open In App

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. 
 




# 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:
     
    # printing the exception
    print(e)

Output : 
 



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

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




# 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:
     
    # printing the exception
    print(e)

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'))}

 


Article Tags :