import logging
logging.basicConfig(filename="std.log",
format='%(asctime)s %(message)s',
filemode='w')
logger=logging.getLogger()
logger.setLevel(logging.INFO)
try:
x+1
except Exception as e:
print(e)
logger.info(e)
--> name 'x' is not defined
try:
10/ 0
except Exception as e:
print(e)
--> division by zero
try:
while True print('Hello world')
except Exception as e:
print(e)
logger.info(e)
--> File "<ipython-input-36-3d08f684f115>", line 2
while True print('Hello world')
^
SyntaxError: invalid syntax
SyntaxError不会触发except
参考:
[1] https://docs.python.org/3/tutorial/errors.html