简体   繁体   English

赶上并继续吗? C#

[英]Catch and Continue? C#

Is a simple question that seeks a simple answer. 是一个简单的问题,需要一个简单的答案。 No code is needed as a demonstration. 无需任何代码即可进行演示。 When i call a function it returns an exception and the whole function stops. 当我调用一个函数时,它返回一个异常,整个函数停止。 How can I ignore the exception and continue the function? 如何忽略异常并继续执行功能?

You cannot ignore the exception. 您不能忽略该异常。

If you do not catch it then the exception will propogate up the call stack until somebody does catch it and handle it, or it reaches the top of the call stack and your program halts. 如果您没有捕获它,那么异常将提升调用堆栈,直到有人捕获并处理它,或者它到达调用堆栈的顶部并且程序停止为止。

To avoid that, you simply catch the exception and decide how to handle it. 为了避免这种情况,您只需捕获异常并决定如何处理它。 If handling it means doing nothing it then simply ... do nothing when you catch the exception: 如果处理意味着不采取任何措施,则只需...在捕获异常时不采取任何措施:

try
{
    SomeFnWhichThrowsAnException();
}
catch
{
    // NO-OP
}

The // NO-OP comment (short of "No-Operation") is an indicator I use to indicate that the "handling" of the exception is to deliberately do nothing, to avoid any potential misunderstanding on the part of anyone reading suh code in the future and interpreting an empty catch block as an error or an oversight. // NO-OP注释(“ No-Operation”的缩写)是一个指标,我用来表明对异常的“处理”是刻意不执行任何操作,以避免任何阅读suh代码的潜在误解将来将空的catch块解释为错误或疏忽。

It should be mentioned that even with a comment and a "good reason" to do nothing in response to an exception, this is highly suspect and is a very bad code smell. 应该提到的是,即使有注释和“正当理由”不对异常做出反应,这也是高度可疑的,并且是非常不好的代码味道。

It may be more common to specifically ignore very specific exceptions or to do so only in specific circumstances, but to ignore every possible exception is highly unadvisable (consider that this will include exceptions such as stack overflows or out of memory conditions etc). 专门忽略非常具体的异常或仅在特定情况下才这样做可能更常见,但是忽略每个可能的异常是极不可取的(考虑到这将包括异常,例如堆栈溢出或内存不足等)。

try
{
    MyFunctionThatErrors();
}
catch{}

You can use a try {..} catch {..} statement. 您可以使用try {..} catch {..}语句。 Here's the reference docs. 这是参考文档。 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch

A try...catch statement should do this job: try ... catch语句应完成以下工作:

try {
    // your code that might throw an exception here
} catch {

}
// code here will execute even if there is an exception

However , try...catch statements are not designed to act as a flow control statement. 但是 ,try ... catch语句并非旨在用作流控制语句。 You shouldn't just ignore the exception without a good reason. 您不应无缘无故地忽略该异常。 You should avoid the exception being thrown in the first place. 您应该避免首先引发异常。

For example, Convert.ToInt32 can throw an exception if the string parameter is in the wrong format. 例如,如果字符串参数格式错误,则Convert.ToInt32可能引发异常。 You shouldn't use try...catch here as a way to detect invalid user input. 您不应在此处使用try ... catch作为检测无效用户输入的方法。 You should check whether the input is valid using some other method, like regex for example. 您应该使用其他方法检查输入是否有效,例如regex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM