Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果Try/catch中没有捕获,则运行代码_C# - Fatal编程技术网

C# 如果Try/catch中没有捕获,则运行代码

C# 如果Try/catch中没有捕获,则运行代码,c#,C#,当我使用Try/Catch时,如果没有检测到错误并且没有捕获,是否有一种与If/Else类似的方法来运行代码 try { //Code to check } catch(Exception ex) { //Code here if an error } //Code that I want to run if it's all OK ?? finally { //Code that runs always } 在try块的末尾添加代码。显然,只有在之前没有例外的情

当我使用Try/Catch时,如果没有检测到错误并且没有捕获,是否有一种与If/Else类似的方法来运行代码

try
{
    //Code to check
}
catch(Exception ex)
{
    //Code here if an error
}

//Code that I want to run if it's all OK ?? 

finally
{
    //Code that runs always
}

try
块的末尾添加代码。显然,只有在之前没有例外的情况下,您才能到达:

try {
  // code to check

  // code that you want to run if it's all ok
} catch {
  // error
} finally {
  // cleanup
}

您可能应该改变捕获方式,只捕获您期望的异常,而不是全部捕获,这可能包括在»代码中抛出的异常,如果一切正常«。

只需将其放在可能抛出异常的代码之后即可

如果抛出异常,它将不会运行;如果未抛出异常,它将运行

try
{
    // Code to check
    // Code that I want to run if it's all OK ??  <-- here
}
catch(Exception ex)
{
    // Code here if an error
}
finally
{
    // Code that runs always
}
试试看
{
//要检查的代码

//如果一切正常,我想运行的代码???如果您需要在try代码成功时始终执行该代码,请将其放在try块的末尾。只要try块中的前一个代码正常运行,它就会运行

try
{
    // normal code

    // code to run if try stuff succeeds
}
catch (...)
{
    // handler code
}
finally
{
    // finally code
}
如果您的“成功”代码需要其他异常处理,您可以始终嵌套try/catch:

try
{
    // normal code

    try
    {
        // code to run if try stuff succeeds
    }
    catch (...)
    {
        // catch for the "succeded" code.
    }
}
catch (...)
{
    // handler code
    // exceptions from inner handler don't trigger this
}
finally
{
    // finally code
}
如果您的“成功”代码必须在结束后执行,请使用变量:

bool caught = false;
try
{
    // ...
}
catch (...)
{
    caught = true;
}
finally
{
    // ...
}

if(!caught)
{
    // code to run if not caught
}

我会这样写:如果对方法的调用运行良好,那么成功只是
try

try 
{
    DoSomethingImportant();
    Logger.Log("Success happened!");
}
catch (Exception ex)
{
    Logger.LogBadTimes("DoSomethingImportant() failed", ex);
}
finally 
{
    Logger.Log("this always happens");
}

为什么不在try结束时添加代码呢?因为您可能希望通过外部异常处理捕获其余代码引发的任何异常,而不是通过内部try/catch块。在这种情况下,最好的方法是这样做(据我所知)就是使用bool。伙计,手指也很快。你们应该去伦敦,在一百万个键盘上用一百万个StackOverflow帐户的一百万只猴子可能会很快给出正确的答案:)我认为这不是一个很好的解决方案,特别是对于内置错误类型。正如你所说的“您只捕获预期的异常"。因此,如果我希望
footexception
来自
//要检查的代码
,那么我只在
try
块中包含该代码。我不希望
footexception
来自
//如果一切正常,您希望运行的代码
,但您的解决方案仍然会错误地捕捉到它。这对于generi来说尤其是一个问题然而,如果所有函数都有自己独特的错误类型,那么您的解决方案将是完美的。
try 
{
    DoSomethingImportant();
    Logger.Log("Success happened!");
}
catch (Exception ex)
{
    Logger.LogBadTimes("DoSomethingImportant() failed", ex);
}
finally 
{
    Logger.Log("this always happens");
}