A little bit confused with the raising exceptions. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

A little bit confused with the raising exceptions.

# the questions: Which errors occur in this code? try: print(1 / 0) except ZeroDivisionError: raise ValueError # answer ZeroDivisionError and ValueError according to @Solid State: According to the question, it is true that both errors OCCUR, but it is also true that they're all included in the output because 'raise' is indented within the except block. Otherwise, ZeroDivisionError is not the part of the output. (Of course some statements should be in the except block.) I.E.: The output are different with or without the indentation before the raise expression. However, I didn't notice any difference in my test. Can anybody help me with that?

5th Feb 2018, 2:29 PM
Abradolf Lincler
Abradolf Lincler - avatar
1 Answer
+ 3
The ZeroException error is put in a queue (First In First Out collection of errors). Then a search for something to handle that exception begins. ZeroException exclaims “Finally something that understands me!” Alas inside the handler for ZeroException another error is added to the queue. Only this time the raise was inside an exception handler and not surrounded by its own try statement. When ValueError seeks to be understood, it finds nothing. ValueErrors sad story ends. But ValueError will not go silently in this dark console. If it cannot be handled, it will take everyone to the console with it. But Seriously: The answer from @SolidState could be improved by saying if raise ValueError had a try statement and an exception handler it would not print to the screen. Since this doesn’t happen, all the errors that are ultimately unhandled come out. Here I add another try-catch inside the error handler and everything is ‘fine’. https://code.sololearn.com/cwa5ZfW7wJ6C/?ref=app
6th Feb 2018, 10:18 AM
SQrL
SQrL - avatar