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,050 points
41 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,500 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
• 9,660 points
23 views
0 votes
1 answer

What does eval() in Python do?

The eval function lets a Python program ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 56,120 points
24 views
0 votes
1 answer

What does hash do in python?

A hash is an fixed sized integer ...READ MORE

answered Oct 15, 2018 in Python by SDeb
• 9,660 points
18 views
0 votes
1 answer

What is the use of “assert” keyword in Python?

You can try the following in a ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 56,120 points
29 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
22 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,500 points
29 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.