Multi Exception | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

10th Oct 2023, 10:18 PM
Annihilate
Annihilate - avatar
6 Answers
+ 4
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
17th Oct 2023, 6:16 PM
Lothar
Lothar - avatar
+ 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 ?
11th Oct 2023, 6:30 PM
Lothar
Lothar - avatar
+ 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)
11th Oct 2023, 8:24 PM
Annihilate
Annihilate - avatar
+ 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
17th Oct 2023, 8:57 PM
Annihilate
Annihilate - avatar
0
…Lothar?
17th Oct 2023, 12:03 AM
Annihilate
Annihilate - avatar
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.
15th Dec 2023, 2:52 AM
Rain
Rain - avatar