Python Programming – Class XII State Syllabus Guide
XII Class Part 1: Exception Handling Program 1: Syntax Error vs Exception # Syntax Error Example (won't run) # print("Hello # Exception Example (will run but gives error) a = 5 b = 0 try : print (a / b) except ZeroDivisionError : print ( "You cannot divide by zero!" ) Program 2: Try-Except-Else-Finally try : num = int ( input ( "Enter a number: " )) print ( 100 / num ) except ValueError : print ( "That's not a number!" ) except ZeroDivisionError : print ( "Zero is not allowed!" ) else : print ( "Division successful!" ) finally : print ( "End of program." ) Program 3: Raising Exceptions def check_age ( age ): if age < 18 : raise ValueError ( "You must be 18 or older!" ) return True try : check_age ( 15 ) except ValueError as e : print ( "Exception:" , e ) Program 4: User-Defined Exception class UnderAgeError ( Excep...