1try:
2 with open(filepath,'rb') as f:
3 con.storbinary('STOR '+ filepath, f)
4 logger.info('File successfully uploaded to '+ FTPADDR)
5except Exception as e: # work on python 3.x
6 logger.error('Failed to upload to ftp: '+ str(e))
1>>> try:
2... raise Exception('spam', 'eggs')
3... except Exception as inst:
4... print(type(inst)) # the exception instance
5... print(inst.args) # arguments stored in .args
6... print(inst) # __str__ allows args to be printed directly,
7... # but may be overridden in exception subclasses
8... x, y = inst.args # unpack args
9... print('x =', x)
10... print('y =', y)
11...
12<class 'Exception'>
13('spam', 'eggs')
14('spam', 'eggs')
15x = spam
16y = eggs
17
1try:
2 # Code to test / execute
3 print('Test')
4except (SyntaxError, IndexError) as E: # specific exceptions
5 # Code in case of SyntaxError for example
6 print('Synthax or index error !')
7except :
8 # Code for any other exception
9 print('Other error !')
10else:
11 # Code if no exception caught
12 print('No error')
13finally:
14 # Code executed after try block (success) or any exception (ie everytime)
15 print('Done')
16
17# This code is out of try / catch bloc
18print('Anything else')