try catch 中未处理的异常会影响性能 [英] unhandled exception at try catch affect performance

查看:65
本文介绍了try catch 中未处理的异常会影响性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 桌面应用程序
我已经编写了一些代码,以便在执行该代码时它会落入 catch 块

I am working in a C# Desktop application
I had made some code such that on execution of that code it falls into the catch block

try
{

        //come code

}
catch(exception ex)
{

}
  //some code 2

我没有处理异常的地方,因为我想在 catch 块之外执行代码
'一些代码 2'

where I have not handled the exception because i want to excecute the code outside the catch block
'some code 2'

但在取消处理异常时会降低执行性能

but on unhandling the exception it decreases the performance of execution

所以请为此提供任何其他解决方案

so please any alter solution for this

推荐答案

不要对正常程序流使用异常.异常并未针对速度进行优化,因为它们通常用于性能不再重要的情况.

Don't use exceptions for normal program flow. Exceptions are not optimised for speed, because they are normally used in situations where the performance no longer matters.

创建异常时,它会收集大量可用于对错误进行故障排除的信息,因此需要一些时间.如果您在调试模式下编译代码,它将收集更多信息,花费更多时间.

When the exception is created, it gathers a lot of information that could be used to troubleshoot the error, so that takes some time. If you have compiled the code in debug mode it will collect even more information, taking even more time to do so.

无论您在做什么导致异常,请尝试在导致实际错误之前捕获该条件.例如,如果您正在捕获除以零,则应在进行除法之前检查要除以的值.

Whatever you are doing that causes the exception, try to catch that condition before it causes the actual error. For example if you are catching a division by zero, you should check the value that you are dividing by before doing the division.

这篇关于try catch 中未处理的异常会影响性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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