0
Multi Exception
I thought exceptions could cause an infinite loop, but my attempt didn’t work. How can exceptions cause infinite loops? Link: https://sololearn.com/compiler-playground/cALno6QlwDa5/?ref=app
6 Antworten
+ 5
Trey ,
i have done a sample, see the attached file.
it is running in a loop, so that users can do repeated input when an exception is raised. (keep in mind that the loop allows repeated input, but sololearn has some limitations with interactive inputs)
https://code.sololearn.com/ccT8nKIIeGDr/?ref=app
+ 3
Trey ,
your code looks like a try without any real task to solve.
> the code as it is creates an expeption during handling another exception. run the code and read the error message carefully.
> can you give a clear task description with real values for input / output ?
+ 1
Well, I thought one of the raise keywords would trigger the other raise, which would trigger the first, then the second and so on, like indirect recursion. Because of this, I thought the variable x would approach infinity, but it ended at two. So what should I do? (P.S., I used ValueError and TypeError because they often get mixed up)
+ 1
#Hmm, that helps. But how could try and except reference itself? Like this code below:
x=0
try:
x+=1
raise ValueError
except ValueError:
x+=1
finally:
print(x)#ouputs 2
#also see my comment in python developer>exceptions>module 10 quiz>last question
0
…Lothar?
0
New Game ,
The error message says it stopped the program at line 7, which is the first time you raised a new exception while handling another. It can't go further than that, so it can't loop.
I'm still reading through the exception docs on python.org, which seem written more for people who write compilers than for regular programmers, and finding it largely inscrutable, but my guess is that the designers figured that an exception within an exception is serious enough to warrant slamming on the brakes and exiting the program.