当我们抛出一个对象/变量来捕获时会发生什么?
两个问题 1)当一个对象/变量被抛出来捕获时会发生什么?比如说,
int foo() {
FILE *fp = ....;
int dummy = 10;
int *dummy_ptr = new int[10];
throw 1;
}
int main() {
try {
foo();
} catch (int &i) {
std::cout<<"ERROR, the value is "<<i<<std::endl;
}
}
在这种情况下,这里会发生什么?创建了一个新变量然后传递了???
如果我使用没有引用的指针或变量怎么
办 catch(int *i) // 或 catch (int i)
另外,作用域内声明或启动的所有变量/资源是否已被释放/关闭?
2)同样在重新抛出的情况下, 如果我打算使用引用重新抛出,则第二个 catch 会获取一个新变量,如果我在没有引用(即按值)的情况下重新抛出,则中间抛出中所做的更改不会受到影响......
int goo() {
throw 2;
}
int foo() {
try{
goo();
} catch(int &i) { // (or) catch(int i) // i is not changing in the next line.
i = 2;
throw;
}
}
int main() {
try {
foo();
} catch (int &i) {
std::cout<<"ERROR, the value is "<<i<<std::endl;
}
}
输出: catch(int &i) // 打印 2 catch(int i) // prints 1
根据我的判断,
我认为只要是引用,值就会受到影响, 如果它在中间步骤中“按值传递”。它仍然将原始对象扔给第二个捕获。
(即)变量的控制流实际上不会抛出中间捕获......
Two Questions
1) What happens when an Object/variable is thrown to catch? Say for example,
int foo() {
FILE *fp = ....;
int dummy = 10;
int *dummy_ptr = new int[10];
throw 1;
}
int main() {
try {
foo();
} catch (int &i) {
std::cout<<"ERROR, the value is "<<i<<std::endl;
}
}
In this situation, what happens here? A new variable created and then passed???
what if I use a pointer or a variable without reference
like
catch(int *i) // or catch (int i)
Also, does all the variables/resources declared or initiated inside the scope has been freed/closed?
2) Also in the case of rethrow,
if I plan to rethrow with a reference, the second catch gets a new variable, if I i rethrow with without reference (i.e) by value, then the changes done in the intermediate throw is not affected....
int goo() {
throw 2;
}
int foo() {
try{
goo();
} catch(int &i) { // (or) catch(int i) // i is not changing in the next line.
i = 2;
throw;
}
}
int main() {
try {
foo();
} catch (int &i) {
std::cout<<"ERROR, the value is "<<i<<std::endl;
}
}
OUTPUT:
catch(int &i) // prints 2
catch(int i) // prints 1
From my judgment,
What I think is, as long as it is reference, the value gets affected,
if its 'pass by value' in the intermediate step. it still throws the original object to the second catch.
(i.e) the control flow for the variable is really not throw the intermediate catch.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的;当你抛出一个对象时,它会在某处创建,然后在处理异常后销毁(即在离开
catch
块后而不重新抛出)。如果您按值捕获,那么您将获得该对象的副本 - 如果您重新抛出异常,那么下一个处理程序将获得原始的新副本,并且不会看到任何您可能已经做出的改变。通过引用捕获将为您提供对抛出对象的引用 - 如果您重新抛出,则下一个处理程序将看到您所做的任何更改。您无法通过指针捕获对象 - 如果抛出指针,您只能捕获指针。
当抛出异常时,抛出的范围和所有封闭范围内的所有自动变量都会被销毁,直到到达处理程序。动态分配的变量(例如
new int[10]
)不会被删除,并且肯定不会调用像fclose
这样的任意清理函数对于 FILE* 变量,除非它们由基于范围的对象(例如智能指针)管理。Yes; when you throw an object it's created somewhere, and then destroyed once the exception has been handled (that is, after leaving the
catch
block without rethrowing).If you catch by value then you'll get a copy of that object - if you rethrow the exception, then the next handler will get a new copy of the original, and won't see any changes you might have made. Catching by reference will give you a reference to the thrown object - if you rethrow, then the next handler will see any changes you made. You can't catch the object by pointer - you'll only catch a pointer if a pointer was thrown.
When an exception is thrown all automatic variables are destroyed, in the scope of the
throw
and all enclosing scopes until the handler is reached. Dynamically allocated variables (such as yournew int[10]
) are not deleted, and arbitrary clean-up functions likefclose
are certainly not called forFILE*
variables, unless they are managed by a scope-based object such as a smart pointer.关于这一点的一点说明,
您在
dummy_ptr*
中的内存将不会被释放,并且您的FILE
指针fp*
将不会被关闭。One note on this,
your memory in
dummy_ptr*
will not be deallocated, and yourFILE
pointerfp*
will not be closed.我认为你不能称它为变量;它没有名字。但一个
在未指定的位置创建了
int
类型的新对象通过实施。当你通过引用捕获时,引用是
绑定到该隐藏对象。当你从钩子末端掉下来时
阻止,或者通过除了重新抛出之外的任何方式离开 catch 块
同样的异常,对象被“释放”。
I don't think you can call it a variable; it doesn't have a name. But a
new object of type
int
is created, in an unspecified place determinedby the implementation. When you catch by reference, the reference is
bound to that hidden object. And when you fall off the end of the catch
block, or leave the catch block by any means other than rethrowing the
same exception, the object is “freed”.