注意: 正确使用 'uncaughtException'
'uncaughtException'
是用于异常处理的粗略机制,仅用作最后的手段。
事件不应该用作 On Error Resume Next
的等价物。
未处理的异常本质上意味着应用程序处于未定义状态。
在没有从异常中正确恢复的情况下尝试恢复应用程序代码可能会导致其他不可预见和不可预测的问题。
从事件句柄中抛出的异常将不会被捕获。 而是,该进程将以非零退出码退出,并将打印堆栈跟踪。 这是为了避免无限递归。
尝试在未捕获异常后正常恢复类似于升级计算机时拔掉电源线。 十有八九,什么都没有发生。 但是第十次,系统损坏了。
'uncaughtException'
的正确用法是在关闭进程之前对分配的资源(例如文件描述符、句柄等)执行同步清理。
在 'uncaughtException'
之后恢复正常操作是不安全的。
为了以更可靠的方式重新启动崩溃的应用程序,无论 'uncaughtException'
是否触发,都应该在单独的进程中使用外部监视器来检测应用程序故障并根据需要恢复或重新启动。
'uncaughtException'
is a crude mechanism for exception handling
intended to be used only as a last resort. The event should not be used as
an equivalent to On Error Resume Next
. Unhandled exceptions inherently mean
that an application is in an undefined state. Attempting to resume application
code without properly recovering from the exception can cause additional
unforeseen and unpredictable issues.
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.
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'
.
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.