处理模块中的异常

时间:2019-04-08 05:57:28

标签: python

我正在尝试使用从https://github.com/dbr/tvdb_api导入“ thetvdb”的模块。我设法将模块导入代码中,但是我试图从模块中获取错误以了解结果。显示未找到,未找到剧集等,并且我无法成功完成该操作而不破坏脚本。

这是我当前的代码:

#!/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")

我想要的东西比我正在打印的东西还要多。我尝试过返回,但是python告诉我它不在函数中。我认为让我失望的是错误类为空。

这是tvdbapi.py文件中的异常类的摘要:

## 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

1 个答案:

答案 0 :(得分:1)

您可能还希望打印该异常:

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