Đối với code cho trước, giải pháp như sau:
Ví dụ
class CustomValueError(ValueError):
def __init__(self, arg):
self.arg = arg
try:
a = int(input("Nhập một số: "))
if not 1 < a < 10:
raise CustomValueError("Giá trị phải từ 1 đến 10.")
except CustomValueError as e:
print("CustomValueError Exception!", e.arg)
Kết quả
Nhập một số: 45 CustomValueError Exception! Giá trị phải từ 1 đến 10. Process finished with exit code 0