处理模块中的异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了处理模块中的异常相关的知识,希望对你有一定的参考价值。

我正在尝试使用我从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
答案

您可能还想打印例外:

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

以上是关于处理模块中的异常的主要内容,如果未能解决你的问题,请参考以下文章

Springboot集成Common模块中的的全局异常处理遇见的问题

处理模块中的异常

php中的错误异常处理

如何使用模块化代码片段中的LeakCanary检测内存泄漏?

片段中的Android致命异常

处理包/模块中的错误时如何处理Python异常