‘raise’
statement is used to raise an exception.
Syntax
raise_stmt
::= "raise" [expression
["from" expression]]
If no
expressions are present, raise re-raises the last exception that was active in
the current scope. If no exception is active in the current scope, a
RuntimeError exception is raised indicating that this is an error.
test.py
while True: try: dividend = int(input("Enter divisor ")) divisor = int(input("Enter dividend ")) if divisor==0: raise Exception("divisor shouldn't be zero") print(dividend/divisor) break except Exception as inst: print(inst)
$ python3 test.py Enter divisor 2 Enter dividend 0 divisor shouldn't be zero Enter divisor 23 Enter dividend 43 0.5348837209302325
raise Exception("divisor shouldn't be
zero")
‘raise’
statement throws Exception, when divisor is 0.
No comments:
Post a Comment