1try:
2 # Dangerous stuff
3except ValueError:
4 # If you use try, at least 1 except block is mandatory!
5 # Handle it somehow / ignore
6except (BadThingError, HorrbileThingError) as e:
7 # Hande it differently
8except:
9 # This will catch every exception.
10else:
11 # Else block is not mandatory.
12 # Dangerous stuff ended with no exception
13finally:
14 # Finally block is not mandatory.
15 # This will ALWAYS happen after the above blocks.
1import sys
2try:
3 S = 1/0 #Create Error
4except: # catch *all* exceptions
5 e = sys.exc_info()
6 print(e) # (Exception Type, Exception Value, TraceBack)
7
8############
9# OR #
10############
11try:
12 S = 1/0
13except ZeroDivisionError as e:
14 print(e) # ZeroDivisionError('division by zero')
1def FileCheck(fn):
2 try:
3 open(fn, "r")
4 return 1
5 except IOError:
6 print "Error: File does not appear to exist."
7 return 0
8
9result = FileCheck("testfile")
10print result