+ 1

Why do we get ZeroDivisionError also as output for the code:

Is not it supposed to be skipped? try: print(1/ 0) except ZeroDivisionError: raise ValueError

13th Feb 2019, 9:15 AM
Kashif Nawaz
Kashif Nawaz - avatar
3 Respuestas
+ 2
The problem is that during handling of the ZeroDivisionError that is caused by 1/0, another exception is raised (the ValueError you raise in the last line). try/catch is supposed to catch exceptions, not to produce more or other kinds of exceptions. Thus python informs you that during handling of the ZeroDivisionError another exception (of type ValueError) is raised. This is supposed to enable you to trace back where the "original" exception (ZeroDivisionError) happened. The Python interpreter tries to help you by reconstructing the whole "chain" of exceptions.
13th Feb 2019, 10:02 AM
Anna
Anna - avatar
+ 1
try/except is called try/catch in other languages, that's why I wrote try/catch. The pattern is the same: you "try" something and if an exception is raised, you "catch" it. If you don't catch an exception, the program will just show an error message and stop. With an except statement, you can tell the program what to do when an exception occurs. In your except statement, you raise yet another exeption which isn't handled. That's why the program stops. The rest is explained in my first post
13th Feb 2019, 5:43 PM
Anna
Anna - avatar
0
I still don't get it? What are u talking about try/catch, I did not use that. Otherwise, except skips the errors and does not show, then why not here?
13th Feb 2019, 3:03 PM
Kashif Nawaz
Kashif Nawaz - avatar