Java Exception vs C++ Exceptions

后端 未结 6 1343
一向
一向 2021-01-12 01:15

Where are exceptions stored ? Stack, Heap. How is memory allocated and deallocated for Exceptions? Now if you have more than one exception which needs to be handled are the

6条回答
  •  情话喂你
    2021-01-12 01:55

    I would assume that memory for exceptions is allocated the same way as for all other objects (on the heap).

    This used to be a problem, because then you cannot allocate memory for an OutOfMemoryError, which is why there was no stack trace until Java 1.6. Now they pre-allocate space for the stacktrace as well.

    If you are wondering where the reference to the exception is stored while it is being thrown, the JVM keeps the reference internally while it unwinds the call stack to find the exception handler, who then gets the reference (on its stack frame, just like any other local variable).

    There cannot be two exceptions being thrown at the same time (on the same thread). They can be nested, but then you have only one "active" exception with a reference to the nested exception.

    When all references to the exception disappear (e.g. after the exception handler is finished), the exception gets garbage-collected like everything else.

提交回复
热议问题