Try-Catch-Custom-Error

Sat 17 May 2025

title: "Try Catch with Custom Error" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


class NegativeMarksError(Exception):
   pass
def check_marks(mark):
    try:

        if(mark < 0):
            raise NegativeMarksError

        if(mark > 50):
            print('pass')
        else:
            print('fail')
    except ValueError:
        print('ValueError: Some value error')
    except TypeError:
        print('TypeError: Cannot convert into int')
    except NegativeMarksError:
        print('Negative Marks are not allowed')
check_marks(10)
fail
check_marks('10')
TypeError: Cannot convert into int
check_marks(-10)
Negative Marks are not allowed


Score: 5

Category: basics