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

查看:128
本文介绍了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 。现在,他们也为stacktrace预先分配空间。

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天全站免登陆