<分区> 分区>
在这个例子中,我想在 try block 中写入 到 console (std.out); 但是当 except block 抛出异常时,我想将异常内容写入 std.err ,它被重定向到日志 file.
我可以在 except block 中打开一个 file 并在上面 write 。但这需要为每个 try-catch block 完成,因此如果有替代方案可以将这些异常 redirecting 到 std.err并记录 std.error。
>>> try:
... print "hello world"
... 1/0
... except ZeroDivisionError as e:
... print "exception"
...
hello world
exception
