在项目中,后端接口如果不进行异常处理那遇到所有问题(后端中的问题)都会自动被处理成顶级异常并返回状态码500(server internal error),这样无论是Debug还是前端提示用户都不清楚。
异常处理的好处
对于整个代码逻辑,如果没有在底层进行异常处理,异常就会一级一级往上抛,直到最外层的。
如果在一个大的系统或服务代码中,不能因为一个小bug就导致整个功能停止。可以预见到的局部且不会影响后续代码逻辑的异常就在本地接受并进行处理,将出现的异常记录到log中,仅有coder可以在日志中进行查看看。如果是会影响到后续代码执行或者是严重的错误,就直接在local让程序停下来没必要继续执行了。
建议的做法
-
To catch all unhandled exceptions by hooking to the
Application.ThreadException event
, then decide :- For a UI application: to pop it to the user with an apology message (winforms)
- For a Service or a Console application: log it to a file (servi