全局处理应用程序中的多个try catch块 [英] Handle multiple try catch blocks across the application globally

查看:358
本文介绍了全局处理应用程序中的多个try catch块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net c#应用程序,在生产中运行得非常好。这是一个庞大的项目,几乎每个方法都尝试使用catch块而没有异常处理(catch中没有代码),现在如果发生任何错误而不是调试所有代码并发现异常,我想全局处理它。有什么建议?

I have an asp.net c# application, running pretty good in production. It's a vast project and almost every method has try catch block with no exception handling(no code in catch), now if any error occurs instead of debugging all code and find exception, I want to handle it globally. Any suggestions?

推荐答案

正如您所说 - 大多数try-catch块都没有任何异常处理,所以他们唯一能做的就是隐藏任何错误(异常)你的代码中有...

没有充分的理由不要使用try-catch!决不!!! Try-catch是非常昂贵的,并且仅用于例外错误...不要将它用作例如输入验证的解决方案或克服丢失文件的问题等等......



如果要在一个地方处理所有unhandledexceptions,请使用Global.asax中的Application_Error方法...

https://msdn.microsoft.com/en-us/library/24395wz3(v=vs。 140).aspx [ ^ ]
As you already stated - most of your try-catch blocks have no any exception handling, so the only thing they do is hiding any error (exception) you have in your code...
Do not use try-catch without good reason!!! Never!!! Try-catch is very expensive and for that it intended for exceptional errors only...Do not use it as a solution for input validation for instance or to overcome problems of missing files and so on...

If you want to handle all unhandledexceptions in one place utilize the Application_Error method in Global.asax...
https://msdn.microsoft.com/en-us/library/24395wz3(v=vs.140).aspx[^]


我认为你需要阅读几篇文章。 [ ^ ],这个 [ ^ ]和这个 [ ^ ]
I think you need to read a couple articles. this[^], this[^] and this[^]


这篇关于全局处理应用程序中的多个try catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆