Hi!
I think I can answer this - Let us say we are making use of Python 2.6 for this scenario.
As of my understanding, you want the finally block to be matched with the block where the exception y resides, correct?
So the finally block is basically matched to the first try block and not the consecutive ones is what you should note.
But, then again if you wanted to add an except block which is present in the inner try block - then in this case alone the finally block will raise exception B.
Check out this code:
try:
raise Exception("x")
except:
try:
raise Exception("y")
except:
pass
finally:
raise
And this is the output to the code:
Traceback (most recent call last):
File "test.py", line 5, in <module>
raise Exception("y")
Exception: y
Let me give you another example for better clarity:
try:
raise Exception("x")
except:
try:
raise Exception("y")
except:
raise
Now check out the output:
Traceback (most recent call last):
File "test.py", line 7, in <module>
raise Exception("y")
Exception: y
So as you see here, basically what we did is that by replacing the finally block with except we ended up raising the exception B as per your requirement.
Hope this helped!