Scope of variables in try catch blocks

Variables declared inthe try block can be accessed in the catch block.

This must be wrong. As the below example shows the destructor is correctly called before the catch block.

[code]

#include <iostream>

using namespace std;

struct X { X() { cout << "hello" << endl; }

~X() { cout << "Goodby" << endl; }

void f() { cout <<"f" <<endl; }

};

int main() try {

X x;

throw 5;

} catch (int z) {

x.f();

}

[/code]

Compiled by CC +w2 tt.cc gives no error/warning.

The output is

hello

Goodby

f

.>

[668 byte] By [willoch] at [2007-11-26 10:41:22]
# 1
HiBoth g++ and comeau online seem to agree with you.Paul
Paul_Floyd at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2

If you have a well formed 'main()', like in a real world example, you get also the compile errors from the SUN compiler:

[code]

#include <iostream>

using namespace std;

struct X { X() { cout << "hello" << endl; }

~X() { cout << "Goodby" << endl; }

void f() { cout <<"f" <<endl; }

};

int main()

{

try {

X x;

throw 5;

} catch (int z) {

x.f();

}

}

[/code]

Regards

Arno>

ScAr at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3

Interesting. Did not know that.

I disagree that one is more welformed or real world than the other.

I program in the real world, and I always start with try.

Sometimes you do not have a choice:

[code]struct X { X(int x) { throw x; } };

struct Y {

X x;

Y() try : x(5) { int z= 3; } catch (int a) { throw z; }

};

[/code]

willoch at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 4

> I program in the real world, and I always start with try.

It depends only on the open and close brackets of the main, it has nothing to do with the 'struct' you have declared and defined.

'Normaly' you have more than one statement in the main (at least you have to return some value at the end, if you don't return in the try and all catch blocks directly) and so you have to make these brackets.

So what I want to show you is, that there is a simple workround for your problem, even if this might be a wrong behaviour from the compiler, what at least must be decided from SUN.

ScAr at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 5

The problem is missing error messages. There is no workaround for missing error messages. There are workarounds for compilers that do no accept legal code.

No workaround for compilers that accepts illegal code. You do not know there is a problem.

So you cannot work around them.

The point of my second example was that the missing error messages also occured

int cases where you cannot put more braces in: constructors where initializion of mebers might throw exceptions.

willoch at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 6

ok,

I agree with you that this isn't a workaround for this problem, but I only want to give you a hint how you can work in the actual situation and didn't want start an academical discussion. Because I am not a native english speaker perhaps I choose the wrong words, sorry for that. The problem should be definitely a bug and the guys which implement the compiler should take care about it.

ScAr at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 7

> Variables declared inthe try block can be accessed in

> the catch block.

> This must be wrong. ...

If you have a service contract with Sun, please file a bug report via your service channel. You can be kept informed about progress, and get pre-release versions of the compiler that fix the bug.

If you don't have a service contract with Sun, please file a bug report at bugs.sun.com.

If you prefer not to file the bug report yourself, let me know, and I'll file one for you.

clamage45 at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 8
Thanks for filing the bug via bugs.sun.com. The bug number is 6482562. It should be visible at bugs.sun.com tomorrow.
clamage45 at 2007-7-7 2:53:03 > top of Java-index,Development Tools,Solaris and Linux Development Tools...