How scoping is handled in exceptions
How is the scoping of variables handled during exceptions? I suppose this will be language specific, and answers for any specific language are greatly appreciated. At least maybe the big ones? C++, python, Java
. This is what I mean:
python
try:
for k, v in map.iteritems():
cnf.conf.set( section, k, v )
for i, j in map2.iteritems():
dosomethingelse()
开发者_开发百科 for m in range(10):
morestuff()
except SpecificError:
vars = (k, v, i, j, m)
finally:
vars in scope #?
Or something more complicated, like nested blocks:
try:
try:
for k, v in map.iteritems():
cnf.conf.set( section, k, v )
for i, j in map2.iteritems():
dosomethingelse()
for m in range(10):
morestuff()
except SpecificError:
vars = (k, v, i, j, m)
except:
vars in scope #?
In java, I believe you can not do the following:
try {
String s = "Hello, finally!";
...
}
finally {
System.out.println(s);
}
You must instead do:
String s = null;
try {
s = "Hello, finally!";
...
}
finally {
System.out.println(s);
}
In other words, the scope of the variable is limited to the block in which it is defined.
HTH
精彩评论