1

Sublime Text 的问题在于,当插件解释器中发生异常时,错误消息仅显示在控制台窗格中,如果不打开它很容易错过。没有声音或任何其他可见的迹象能够引起我的注意。

所以我认为的解决方案是创建自己的异常处理函数,该函数可以在向控制台打印异常的同时显示消息错误框。

import sys, sublime, sublime_plugin

def handle_sublime_exceptions(ex_cls, ex, tb):
  sublime.error_message("exception occured")

sys.excepthook = handle_sublime_exceptions

def my_test_exception_function():
  raise Exception("my_test_exception_function")

# uncommenting this line shows correct error box
# my_test_exception_function()

class EventDump(sublime_plugin.EventListener):
  def on_post_save(self, view):
    # here the function raises exception but excepthook
    # is reset to python's default
    # and only console is reacting
    my_test_exception_function()

而且它工作得很好,即使语法错误在sys.excepthook重新声明后发生时也是可见的。

但是当在 sublime 的 api 类中引发异常时,EventDump这种替换不起作用。

有没有办法为 Sublime 插件设置自定义异常挂钩,以便它可以与所有插件代码一起使用?

它是蟒蛇还是崇高的“虫子”?

4

0 回答 0