If you are crazy enough to be creating a new unit test framework, your test runner will probably need to catch java.lang.AssertionError thrown by any test cases.
int length = Integer.parseInt(xyz);
byte[] buffer = new byte[length];
可以产生 OutOfMemoryError,但这是一个运行时问题,没有理由终止您的程序。
NoClassDefFoundError occur mostly if a library is not present or if you work with another Java version. If it is an optional part of your program then you should not terminate your program.
理想情况下,我们不应该处理/捕获错误。但是,在某些情况下,我们可能需要根据框架或应用程序的需求进行操作。假设我有一个 XML 解析器守护进程,它实现了消耗更多内存的 DOM Parser。如果有像解析器线程这样的需求,当它获得 OutOfMemoryError时不应该死亡,相反,它应该处理它并向应用程序/框架的管理员发送消息/邮件。