pure scope and try catch scoping idea

J

John Rivers

try catch blocks create a new scope

i don't think that they should

for example, to add error handling to this statement:

int x = 1;

you have to rewrite it as a seperate declaration and assignment:

int x;
try {
x = 1;
} catch {}

maybe if try catch used different braces to show new scope is not
necessary:

try [
//new scope not needed here
int x = 1;
] catch (Exception exc) {
//new scope needed here
}

this would allow error handling to be non-destructively added and
removed from code

plus it should be possible in C# to do this

int x = 1;
{
int y = 1;
}

ie: have a scope for no other purpose than having a scope

rather than

do {
//create new scope for scopes sake
} while (false)
 
C

Christof Nordiek

Hi John,

comments inlined.

John Rivers said:
try catch blocks create a new scope

i don't think that they should

for example, to add error handling to this statement:

int x = 1;

you have to rewrite it as a seperate declaration and assignment:

int x;
try {
x = 1;
} catch {}

maybe if try catch used different braces to show new scope is not
necessary:

try [
//new scope not needed here
int x = 1;
] catch (Exception exc) {
//new scope needed here
}

Even that wouldn't work. If the assignment failed, x is in an unassigned
state. That's why x would be unusable in the catch clause. You could assign
to it. But reading would result in a compiler error: Use of unassigned
variable. So the assignment has to be inside of the catch clause anyway
this would allow error handling to be non-destructively added and
removed from code

plus it should be possible in C# to do this

int x = 1;
{
int y = 1;
}

ie: have a scope for no other purpose than having a scope


But this already works in C#. I even used it once.
So there is no use for that:
 
B

Brian Gideon

John said:
try catch blocks create a new scope

i don't think that they should

for example, to add error handling to this statement:

int x = 1;

you have to rewrite it as a seperate declaration and assignment:

int x;
try {
x = 1;
} catch {}

maybe if try catch used different braces to show new scope is not
necessary:

try [
//new scope not needed here
int x = 1;
] catch (Exception exc) {
//new scope needed here
}

this would allow error handling to be non-destructively added and
removed from code

plus it should be possible in C# to do this

int x = 1;
{
int y = 1;
}

ie: have a scope for no other purpose than having a scope

rather than

do {
//create new scope for scopes sake
} while (false)

John,

Braces can already be used to create "pure scopes". And, I happen to
like that a new scope is created for try-catch-finally blocks. Plus,
it's easy to remember the rule that braces always narrow the scope.

Brian
 
J

John Rivers

good point re: refering to unassigned variables in catch clause

thanks for pointing out you can already use { } - for some reason I
thought you couldn't


Hi John,

comments inlined.





try catch blocks create a new scope
i don't think that they should
for example, to add error handling to this statement:
int x = 1;
you have to rewrite it as a seperate declaration and assignment:
int x;
try {
x = 1;
} catch {}
maybe if try catch used different braces to show new scope is not
necessary:
try [
//new scope not needed here
int x = 1;
] catch (Exception exc) {
//new scope needed here
}Even that wouldn't work. If the assignment failed, x is in an unassigned
state. That's why x would be unusable in the catch clause. You could assign
to it. But reading would result in a compiler error: Use of unassigned
variable. So the assignment has to be inside of the catch clause anyway


this would allow error handling to be non-destructively added and
removed from code
plus it should be possible in C# to do this
int x = 1;
{
int y = 1;
}
ie: have a scope for no other purpose than having a scopeBut this already works in C#. I even used it once.
So there is no use for that:




rather than
do {
//create new scope for scopes sake
} while (false)- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top