Java异常与C ++异常 [英] Java Exception vs C++ Exceptions

查看:135
本文介绍了Java异常与C ++异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异常存储在哪里?
堆栈,堆。
如何为异常分配和释放内存?
现在如果你有多个异常需要处理,那么所有这些异常的对象都创建了?

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 there objects of all these exceptions created?

推荐答案

我假设异常的内存分配方式与所有其他对象(堆上的)相同。

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

这是一个问题,因为那样你就不能为OutOfMemoryError,
分配内存,这就是为什么会有无堆栈跟踪,直到Java 1.6 。现在他们也为堆栈跟踪预先分配空间。

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.

如果你想知道在 抛出,JVM在内部保持引用,同时展开调用堆栈以找到异常处理程序,然后谁获得引用(在其堆栈框架上,就像任何其他局部变量一样)。

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.

这篇关于Java异常与C ++异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆