RuntimeError | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

RuntimeError

So I decided to run a raise line without an exception, and it returned a RuntimeError, which I have no idea what that is or means. Here’s the code: https://sololearn.com/compiler-playground/c9G4pKvkkgL0/?ref=app Are there other ways to cause this exception?

5th Nov 2023, 4:38 PM
Annihilate
Annihilate - avatar
8 Answers
0
New Game , Ah. If the code is only the word raise, as Lothar said, you have to specify which error you want to raise. But if you use it with try and except, it will re-raise whatever error got caught by the except. try pass # whatever code except # catches every exception pass # whatever code raise # knows which error to re-raise Here's another python.org link about exceptions in addition to sandra's. This one's from the tutorial area. https://docs.python.org/3/tutorial/errors.html#errors-and-exceptions (Syntax errors aren't exceptions.)
21st Nov 2023, 5:38 AM
Rain
Rain - avatar
+ 6
New Game , if we use the *raise* keyword, we have to say *what type of exception* we want to raise. as sandra already mentioned, the tutorial is a good source to learn from. may be this sample can help you also: https://code.sololearn.com/cN33CcVh09sP/?ref=app
5th Nov 2023, 6:27 PM
Lothar
Lothar - avatar
+ 3
I think it makes sence that you read the documentation for exceptions: https://docs.python.org/3/library/exceptions.html
5th Nov 2023, 6:18 PM
sandra
sandra - avatar
+ 2
New Game , I can't read your code on the app but here's what I learned about raise. try: pass except ValueError: pass except IndexError: pass except: raise Normally, you should always use except with a specific exception, such as ValueError, and you should add a separate clause for each error that the code in the try block might automatically raise, such as IndexError, etc. The separate except clauses allow you to handle each error type appropriately, such as printing a usage string for the user, such as, "Usage: Enter a positive integer." However, when you're first writing your code, you might not be aware of all the different errors that your try block might automatically raise. In that case, you can add an except clause with no specified error, and put raise inside it. That will catch any other error that gets raised and re-raise it so it gets printed in the console when you're testing. Then you can add an except clause for that specific error and continue testing.
5th Nov 2023, 8:43 PM
Rain
Rain - avatar
+ 2
Got it
4th Dec 2023, 11:31 PM
Annihilate
Annihilate - avatar
+ 1
Rain The only thing in the code is the word raise
20th Nov 2023, 10:15 PM
Annihilate
Annihilate - avatar
0
Well....what are you raising?
5th Nov 2023, 6:16 PM
Henrik Hultgren
Henrik Hultgren - avatar
0
According to the website from sandra, it’s raised when it doesn’t fall into any of the other categories.
5th Nov 2023, 6:42 PM
Annihilate
Annihilate - avatar