In order to raise an error with a message in Python programming you can make use of the Exception class.
Let's say you want to raise an error when the provided age is less than 18, and the message you want to print is "Error! Age should be greater than 18!"
age = 15
if age < 18:
raise Exception("Error!, age should be greater than 18!")
Output:
Traceback (most recent call last):
File "./example.py", line 4, in <module>
Exception: Error!, age should be greater than 18!
As you can see in the above example: raise is the keyword that you should use to raise an exception.
Let's see another example: If the provided variable type is not str raise an error message.
name = 2k
if not type(name) is str:
raise TypeError("Error! Name should be a String value!")

Have Questions? Post them here!
More Posts related to Python,
- How to Install Python Modules in VS Code
- 3 Python program to add two numbers
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: How to get Current Directory
- Python - Convert float to String
- Tkinter - add x and y padding to label text
- Check if String Contains a Substring - Python
- Read JSON File in Python Program
- How to Convert String to DateTime in Python
- Sorting an array using Bubble Sort in Python Programming
- Python Hello World! Program with code example (snippet)
- How to delete a dir or folder using Python code
- Change label (text) color in tkinter
- Set width and height for the label in tkinter
- Check version of pip package installer for Python
- Reading JSON file in Python with Examples
- Indent Python code in Notepad++
- tkinter - Hello World! Program
- How to resolve Failed to create interpreter PyCharm Error
- Base64 Encoding Decoding in Python Programming
- How to pip install Python Modules in VSCode
- How to take user input from the console in a Python program
- Take input argument from command line in Python Programming
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Calculate discount amount python code
More Posts:
- SharePoint 2016 error - Could not find file ManageUserProfileServiceApplicationTenantSimplified.xml - SharePoint
- Base64 Encoding Decoding in Python Programming - Python
- Fix Microsoft Office 365 error code 135011 - Your organization has disabled this device - Microsoft
- Outlook - The mailbox isn't available. This may have occurred because the license for the mailbox has expired. - Microsoft
- Chrome : When Adobe flash player has finished updating, reload this page to active it - Chrome
- [Fix] zsh: command not found: python on Mac - Python
- [Fix] Docker Run unknown shorthand flag: 'r' in -rm - Docker
- Install SonarLint on Visual Studio Code - HowTos