处理来自modu的异常

2024-05-15 03:41:37 发布

您现在位置: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

Tags: thecomapishowexceptionerrorpassbe

热门问题