xxxxxxxxxx
raise Exception "FileError: \nCould not read file."
xxxxxxxxxx
class UnderAge(Exception):
pass
def verify_age(age):
if int(age) < 18:
raise UnderAge
else:
print('Age: '+str(age))
# main program
verify_age(23) # won't raise exception
verify_age(17) # will raise exception
xxxxxxxxxx
# Custom exception class
class CustomException(Exception):
pass
# Raise the custom exception
raise CustomException("This is a custom exception")
# Try-catch block to handle the custom exception
try:
# Code that may raise the custom exception
raise CustomException("This is a custom exception")
except CustomException as e:
# Handle the custom exception
print("Custom exception occurred:", e)
xxxxxxxxxx
This will work. But it's kind of crazy.
try:
assert False, "A Message"
except AssertionError, e:
raise Exception( e.args )
Why not the following? This is less crazy.
if not someAssertion: raise Exception( "Some Message" )
It's only a little wordier than the assert statement, but doesn't violate our expectation that assert failures raise AssertionError.
Consider this.
def myAssert( condition, action ):
if not condition: raise action