我很想知道我是否应该最小化try/catch块中的代码,否则它确实无关紧要.
public bool ObjectExists(string stringTest, string againSomethingElse)
{
if(true) {}
else {} //Code here is better/worst/same
try
{
//Versus code inside try/catch block
}
catch (Exception)
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
这是解决这个问题的正确方法.
首先编写代码,以便异常处理是正确的.始终保持正确性.
然后设定合理的,以客户为中心的绩效目标.然后测试你的程序.然后,如果您没有达到目标,请使用分析器找到最慢的东西.如果一些奇怪的巧合最慢的事情恰好是你正确的异常处理,那么你才应该考虑异常处理的性能成本是什么.