yusof
yusof

Reputation: 163

Handling exceptions from a module

I'm trying to work with a module i've imported "thetvdb" from https://github.com/dbr/tvdb_api. I managed to import the module into my code but i'm trying to get the errors from the module to know the result. show not found, episode not found, etc and I'm unable to successfully do that without breaking the script.

This is my current code:

#!/usr/bin/env python3
from tvdb_api import tvdb_api
t = tvdb_api.Tvdb()

show_name = raw_input("What is the tv show? ")
season_num = int(raw_input("What is the season number? "))
episode_num = int(raw_input("What is the episode number? "))
try:
        episode = t[show_name][season_num][episode_num] # get season 1, episode 3 of show
        print episode['episodename'] # Print episode name
except tvdb_api.tvdb_exception:
        print("error")

I want something more than what i'm printing. I've tried a return but python told me it's not in a function. What I think is throwing me off is the fact the error classes are empty.

Here is a snippet of the exception classes in the tvdbapi.py file:

## Exceptions

class tvdb_exception(Exception):
    """Any exception generated by tvdb_api
    """
    pass

class tvdb_error(tvdb_exception):
    """An error with thetvdb.com (Cannot connect, for example)
    """
    pass

class tvdb_userabort(tvdb_exception):
    """User aborted the interactive selection (via
    the q command, ^c etc)
    """
    pass

class tvdb_notauthorized(tvdb_exception):
    """An authorization error with thetvdb.com
    """
    pass

class tvdb_shownotfound(tvdb_exception):
    """Show cannot be found on thetvdb.com (non-existant show)
    """
    pass

class tvdb_seasonnotfound(tvdb_exception):
    """Season cannot be found on thetvdb.com
    """
    pass

class tvdb_episodenotfound(tvdb_exception):
    """Episode cannot be found on thetvdb.com
    """
    pass

class tvdb_resourcenotfound(tvdb_exception):
    """Resource cannot be found on thetvdb.com
    """
    pass

Upvotes: 0

Views: 100

Answers (1)

Tiger-222
Tiger-222

Reputation: 7150

You may want to print the exception as well:

try:
    # ...
except tvdb_api.tvdb_exception as exc:
    print("error:", exc)

Upvotes: 1

Related Questions