python custom exception

Solutions on MaxInterview for python custom exception by the best coders in the world

showing results for - "python custom exception"
Alix
14 Aug 2020
1raise Exception "FileError: \nCould not read file."
Edison
28 Jan 2018
1class UnderAge(Exception):
2   pass
3 
4def verify_age(age):
5   if int(age) < 18:
6       raise UnderAge
7   else:
8       print('Age: '+str(age))
9 
10# main program
11verify_age(23)  # won't raise exception
12verify_age(17)  # will raise exception
Paola
08 Aug 2017
1class UnderAge(Exception):
2   pass
3 
4def verify_age(age):
5   if int(age) < 18:
6       raise UnderAge("You must be at least 18 years old")
7   else:
8       print('Age: '+str(age))
9 
10# main program
11verify_age(23)  # won't raise exception
12verify_age(17)  # will raise exception