I'm new to Python and struggling with handling self-defined errors. When my code spots the error, I want it to throw an error in red font and take me back to the Python terminal without killing Python.
I came across sys.exit() looking for an answer, but it quits Python completely. Do you know of an alternative that throws back an error in red font and takes me back to the terminal?
This is what I have so far.
import sys
def do_something(parameter):
if parameter > 100:
# quit the function and any function(s) that may have called it
sys.exit('Your parameter should not be greater than 100!')
else:
# otherwise, carry on with the rest of the code
Please let me know if I'm not clear and I'll be happy to provide more details. Thank you all in advance!