-
Hi there! I wanted to know if there is a solution to catch all exceptions from the whole app running, I tried this code following : if __name__ == "__main__":
try:
app = App()
ret = app.run()
except Exception as err:
print("sad") but it didn't work... I got the exception with rich printing but it's not handled by my try/except in my main. Any ideas ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Generally I think it's fair to say that the best idea is to catch exceptions as close to their source as possible, and then handle them gracefully. Textual, as you can see, has a catch-all handler that reports the error. I don't believe there's a direct method of turning it off, but you could probably override import sys
from textual.app import App
class BadApp(App[None]):
def on_mount(self) -> None:
_ = 1/0
def _fatal_error(self) -> None:
super()._fatal_error()
self._exit_renderables = []
_, exception, _ = sys.exc_info()
if exception is not None:
raise exception
if __name__ == "__main__":
try:
BadApp().run()
except Exception as error:
print(error) This, of course, has the downside of overriding an internal method, but I suspect this will be stable. |
Beta Was this translation helpful? Give feedback.
Generally I think it's fair to say that the best idea is to catch exceptions as close to their source as possible, and then handle them gracefully. Textual, as you can see, has a catch-all handler that reports the error. I don't believe there's a direct method of turning it off, but you could probably override
App._fatal_error
(which is at the heart of this) and do something with it. For example: