当我们抛出一个对象/变量来捕获时会发生什么?

发布于 2025-01-08 07:01:55 字数 1071 浏览 0 评论 0原文

两个问题 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

待"谢繁草 2025-01-15 07:01:55

在这种情况下,会发生什么?创建并传递新变量?

是的;当你抛出一个对象时,它会在某处创建,然后在处理异常后销毁(即在离开catch块后而不重新抛出)。

如果我使用没有引用的指针或变量怎么办?同样在重新抛出的情况下...

如果您按值捕获,那么您将获得该对象的副本 - 如果您重新抛出异常,那么下一个处理程序将获得原始的新副本,并且不会看到任何您可能已经做出的改变。通过引用捕获将为您提供对抛出对象的引用 - 如果您重新抛出,则下一个处理程序看到您所做的任何更改。您无法通过指针捕获对象 - 如果抛出指针,您只能捕获指针。

此外,作用域内声明或启动的所有变量是否都已关闭?

当抛出异常时,抛出的范围和所有封闭范围内的所有自动变量都会被销毁,直到到达处理程序。动态分配的变量(例如 new int[10]不会被删除,并且肯定不会调用像 fclose 这样的任意清理函数对于 FILE* 变量,除非它们由基于范围的对象(例如智能指针)管理。

In this situation, what happens here? A new variable created and then passed?

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).

what if I use a pointer or a variable without reference? Also in the case of rethrow...

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.

Also, does all the variables declared or initiated inside the scope has been closed?

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 your new int[10]) are not deleted, and arbitrary clean-up functions like fclose are certainly not called for FILE* variables, unless they are managed by a scope-based object such as a smart pointer.

动次打次papapa 2025-01-15 07:01:55

是的,当抛出异常时,所有自动变量都会被
销毁,在抛出的范围内和所有封闭的范围内,直到
已到达处理程序。

关于这一点的一点说明,
您在 dummy_ptr* 中的内存将不会被释放,并且您的 FILE 指针 fp* 将不会被关闭。

Yes, 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.

One note on this,
your memory in dummy_ptr* will not be deallocated, and your FILE pointer fp* will not be closed.

陈年往事 2025-01-15 07:01:55

我认为你不能称它为变量;它没有名字。但一个
在未指定的位置创建了 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 determined
by 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”.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文