Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement a custom Python Exception with custom message?
For the given code above the solution is as follows
Example
class CustomValueError(ValueError):
def __init__(self, arg):
self.arg = arg
try:
a = int(input("Enter a number:"))
if not 1 < a < 10:
raise CustomValueError("Value must be within 1 and 10.")
except CustomValueError as e:
print("CustomValueError Exception!", e.arg)
Output
Enter a number:45 CustomValueError Exception! Value must be within 1 and 10. Process finished with exit code 0
Advertisements