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, 2019 in Python by Anirudh
• 2,090 points
1,124 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, 2019 by Nymeria
• 3,560 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,300 points
1,365 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, 2019 in Python by Vinod
1,083 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, 2019 in Python by anonymous
760 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, 2019 in Python by Iris
6,526 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,067 views
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,560 points
1,593 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP