我也在该站点上看到了这段代码,我想在我的项目中实现它之前尝试一下这些代码。但是似乎我很难理解baseException,因为我在python中不太好。这是代码:
from threading import Timer
import time
import sys
class Watchdog:
def __init__(self, timeout, userHandler=None): # timeout in seconds
self.timeout = timeout
self.handler = userHandler if userHandler is not None else self.defaultHandler
self.timer = Timer(self.timeout, self.handler)
self.timer.start()
def reset(self):
self.timer.cancel()
self.timer = Timer(self.timeout, self.handler)
self.timer.start()
def stop(self):
self.timer.cancel()
def defaultHandler(self):
raise self
watchdog = Watchdog(5)
count = 0
while(count < 4):
try:
count = count + 1
print(count)
time.sleep(1)
except Watchdog:
print("I'm a fancy error message")
watchdog.stop()
然后我收到此错误消息:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 1180, in run
self.function(*self.args, **self.kwargs)
File "/home/pi/wdt.py", line 24, in defaultHandler
raise self
TypeError: exceptions must derive from BaseException
我不知道该如何解决这个问题。我在互联网上进行了大量搜索,但没有找到解决方案。请帮我解决这个问题。谢谢。