Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Implement Custom Python Exception พร้อม Custom Message

สร้าง class สืบทอดจาก Exception หรือ subclass ที่เฉพาะเจาะจง

ตัวอย่าง

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)

ผลลัพธ์

Enter a number: 45
CustomValueError Exception! Value must be within 1 and 10.