Catching java.lang.OutOfMemoryError?

前端 未结 14 1449
Happy的楠姐
Happy的楠姐 2020-11-22 06:01

Documentation for java.lang.Error says:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application

14条回答
  •  伪装坚强ぢ
    2020-11-22 06:59

    You can catch anything under Throwable, generally speaking you should only catch subclasses of Exception excluding RuntimeException (though a large portion of developers also catch RuntimeException... but that was never the intent of the language designers).

    If you were to catch OutOfMemoryError what on earth would you do? The VM is out of memory, basically all you can do is exit. You probably cannot even open a dialog box to tell them you are out of memory since that would take memory :-)

    The VM throws an OutOfMemoryError when it is truly out of memory (indeed all Errors should indicate unrecoverable situations) and there should really be nothing you can do to deal with it.

    The things to do are find out why you are running out of memory (use a profiler, like the one in NetBeans) and make sure you don't have memory leaks. If you don't have memory leaks then increase the memory that you allocate to the VM.

提交回复
热议问题