Python中使用 try 语句遇到的问题

1.IndentationError: unexpected indent(语法错误)


bruce@bruce-desktop:~/catkin_ws$ rosrun my_serial_node serial_subscribe.py
  File "/home/bruce/catkin_ws/src/my_serial_node/scripts/serial_subscribe.py", line 56
    try:
    ^
IndentationError: unexpected indent
bruce@bruce-desktop:~/catkin_ws$

解决方法:
按提示在给出错误的行数附近查看,是否缩进有误,从新将这个try方法用一新行写一遍。

2.关于解决 try except这个异常的处理,OSError,TypeError,NameError,Exception 都是关键字,不能瞎写。

except OSError as reason:    
 print('文件出错了T_T')    
 print('出错原因是%s'%str(reason))
 
except TypeError as reason:    
 print('求和出错了T_T')    
 print('出错原因是%s'%str(reason))
 
except (OSError,TypeError,NameError) as reason:

except Exception as e:
        print("出现异常:"+str(e))
posted @ 2022-12-03 23:39  bruce_lp  阅读(190)  评论(0)    收藏  举报