# Run with python3!!! # This demonstrates that Python has exception-handling # with a finally block. # Python also turns keyboard interrupts into exceptions. def demo1(): a=0 while a == 0: try: s = input('enter a number: ') val = int(s) except ValueError: print("not a valid number") continue except KeyboardInterrupt: # Be careful with this. Don't take away your user's ability to Cntr-C out of a program!!! print( "do you really want to stop??" ) continue finally: print( "finally" ) if val == 0: #break a=1 print("terminating") def demo2_helper(): raise Exception( "hi" ) def demo2(): try: demo2_helper() except Exception as e: print("demo 2 catching the error") print(e) demo2()