How to Define What Kind of Error to Raise in Python


In this example we will show how to define what kind of error to raise and the text to print to the user using the raise keyword in Python.

Source Code

num = input('Please enter a number: ')

if not num.isdigit():
    raise ValueError('The input must be a number!')

Output:

Please enter a number: a
Traceback (most recent call last):
  File "demo.py", line 4, in 
    raise ValueError('The input must be a number!')
ValueError: The input must be a number!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments