警告:正确使用 'uncaughtException'


【Warning: Using 'uncaughtException' correctly】

'uncaughtException' 是一种粗略的异常处理机制,仅应作为最后手段使用。该事件不应被用作 On Error Resume Next 的替代方法。未处理的异常本质上意味着应用处于未定义状态。尝试在未正确处理异常的情况下恢复应用代码可能会导致额外的、无法预料的问题。

从事件处理程序内部抛出的异常将不会被捕获。相反,进程将以非零退出代码退出,并打印堆栈跟踪。这是为了避免无限递归。

【Exceptions thrown from within the event handler will not be caught. Instead the process will exit with a non-zero exit code and the stack trace will be printed. This is to avoid infinite recursion.】

在未捕获异常后尝试正常恢复,就像在升级计算机时拔掉电源一样。十次中有九次,什么也不会发生。但第十次,系统就会损坏。

【Attempting to resume normally after an uncaught exception can be similar to pulling out the power cord when upgrading a computer. Nine out of ten times, nothing happens. But the tenth time, the system becomes corrupted.】

'uncaughtException' 的正确用法是在关闭进程之前执行已分配资源的同步清理(例如文件描述符、句柄等)。'uncaughtException' 之后恢复正常操作是不安全的。

【The correct use of 'uncaughtException' is to perform synchronous cleanup of allocated resources (e.g. file descriptors, handles, etc) before shutting down the process. It is not safe to resume normal operation after 'uncaughtException'.

为了以更可靠的方式重启崩溃的应用,无论是否触发 'uncaughtException',都应在一个独立的进程中使用外部监控程序来检测应用故障,并根据需要进行恢复或重启。

【To restart a crashed application in a more reliable way, whether 'uncaughtException' is emitted or not, an external monitor should be employed in a separate process to detect application failures and recover or restart as needed.】