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!")
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!
- Set width and height for the label in tkinter
- pip get list of all outdated Python packages
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- How to uninstall pip Python packages
- Python Hello World! Program with code example (snippet)
- tkinter - Hello World! Program
- Change the background of Tkinter label or text
- Check installed Python version in Windows, Linux & macOS
- Whats new in Python 3.10 Pre-release
- Calculate discount amount python code
- Create Custom Android AlertDialog - Android
- Copy Text to Android Clipboard Programmatically ClipboardManager - Android
- Facebook Down Will Be Back Soon - Facebook
- How to check installed Eclipse version 32-bit or 64-bit - Eclipse
- How to create an Autocomplete Android EditText Example - Android
- How to display Toast on Button Click : Android - Android
- Two Buttons next to each other in Android Layout - Android
- JSON Datatypes : Tutorial - Json-Tutorial
- Eclipse : Workspace was written with an older version of the product and will be updated - Eclipse
- Android Error Unexpected cast to Button: layout tag was FrameLayout - Android
- [Solved] SharePoint System.IO.FileNotFoundException was unhandled - SharePoint
- Fibonacci series from 1 to 500 table - Html
- The author stylesheet specified in tag script is too long - document contains 21759 bytes whereas the limit is 10000 bytes - Html
- Facebook Graph API Unavailable - Facebook