Saturday, February 20, 2010

Exceptional exception handling in C++

#include

using namespace std;

//Global
int a = 20;

void foo()
try {
throw a;
}
catch(int &x)
{
// You can't modify value of "a"
x = 10;
}

int main()
{
foo();
cout<<"A="< }

A=20 !!!!
When you write "throw a", an invisible, phantom exception object gets created. You pass reference to that object and hence any change made to original object have no effect.

1 comment:

睡衣 said...

Since it is the early worm that gets eaten by the bird, sleep late.............................................