howtechstuffworks
howtechstuffworks

Reputation: 1916

What happens when we throw an object/variable to catch?

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

Upvotes: 1

Views: 1914

Answers (3)

Smittii
Smittii

Reputation: 187

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.

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254431

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.

Upvotes: 5

James Kanze
James Kanze

Reputation: 153909

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

Upvotes: 0

Related Questions