Raising Custom Error

'''
Raising Custom Error:
In Python, we can raise custom error by using the "raise" keyword.
'''
#Example:
a = int(input("Enter a value between 5 and 9"))
if(a<5 or a>9):
    raise ValueError("Value should be between 5 and 9")
# output =
# Enter a value between 5 and 9 10
# Traceback (most recent call last):
#   File "c:\Users\sindh\OneDrive\Desktop\Python Codes\Raising Custom Error.py", line 4, in <module>
#     raise ValueError("Value should be between 5 and 9")
# ValueError: Value should be between 5 and 9

salary = int(input("Enter the salary amount: "))
if not 50000 < salary < 70000:
    raise ValueError("Not a valid salary")
# output =
# Enter the salary amount: 10000
# Traceback (most recent call last):
#   File "c:\Users\sindh\OneDrive\Desktop\Python Codes\Raising Custom Error.py", line 18, in <module>
#     raise ValueError("Not a valid salary")
# ValueError: Not a valid salary

'''
Custom Exception:
In Python, we can define custom exceptions by creating a new class that is derived from the built-in exception class.

Syntax:
class CustomError(Exception):
    #code
    pass
    
try:
    #code

except CustomError:
    #code

Raising custom error is important in programming because it makes our code more understandable, robust and easier to debug or maintain.
'''

#Quiz:
a = int(input("Enter a number between 5 and 9"))
if (a == "quit"):
    print("pass")
elif (int(a)<5 or int(a)>9):
    raise ValueError("Value should be between 5 and 9")
 
a = input("Enter the value butween 5 and 9: ")



  
    

Comments

Popular posts from this blog

Print Function

Arithmetic Operators

Sets in Python