What does the Raise keyword do in Python?

+1 vote

Hi all,

My question is fairly simple. Please do check out the following piece of code:

try:
    raise Exception("x")
except:
    try:
        raise Exception("y")
    finally:
        raise

Here I am aware that the Exception: x will be raised. But I want it to raise the exception y.

So my question is that why does the final raise call always raise the primary exception but no the last exception?

Is it how it works or am I missing something here?

All help appreciated!

Jan 25 in Python by Anirudh
• 2,070 points
81 views

1 answer to this question.

0 votes

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!

answered Jan 25 by Nymeria
• 3,520 points

Related Questions In Python

0 votes
1 answer

What does the return statement do in Python?

The print() function is use to write ...READ MORE

answered Oct 1, 2018 in Python by SDeb
• 13,190 points
109 views
0 votes
1 answer

What does the random.triangular(low, high, mode) function do in python?

It returns a random floating point number ...READ MORE

answered May 27 in Python by Vinod
97 views
0 votes
1 answer

What does the map() method do in Python?

The map() function in Python is a ...READ MORE

answered Jun 17 in Python by anonymous
29 views
0 votes
1 answer

What does the penup() do in Python(turtle module)

penup() basically makes sure that the moving ...READ MORE

answered Jun 21 in Python by Iris
50 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 349 views
+4 votes
6 answers
0 votes
1 answer

In NumPy how do I get the maximum of subsets? Python

You can use np.maximum.reduceat: >>> _, idx = np.unique(g, ...READ MORE

answered Nov 9, 2018 in Python by Nymeria
• 3,520 points
80 views