C# 如何强制执行到 Catch 块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9312411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:50:47  来源:igfitidea点击:

How Can I Force Execution to the Catch Block?

c#exception-handling

提问by user983738

I am wondering can try..catchforce execution to go into the catchand run code in there?

我想知道可以try..catch强制执行进入catch并在那里运行代码吗?

here example code:

这里示例代码:

try {
    if (AnyConditionTrue) {
      // run some code
    }
    else {
      // go catch
    }
} catch (Exception) {
    // run some code here...
}

采纳答案by cadrell0

Rather than throwing an Exception in the else, I would recommend extracting the code from your catchinto a method and call that from your else

与其在 中抛出异常else,我建议您将代码从您catch的方法中提取到一个方法中,然后从您的 else 中调用它

try
{
    if (AnyConditionTrue)
    {
        MethodWhenTrue();
    }
    else
    {
        HandleError();
    }
}
catch(Exception ex)
{
    HandleError();
}

回答by user983738

   try{
      if (AnyConditionTrue){
              //run some code
               }
      else{
              throw new Exception();
          }
   }
   catch(){

      //run some code here...

   }

But like Yuck has stated, I wouldn't recommend this. You should take a step back at your design and what you're looking to accomplish. There's a better way to do it (i.e. with normal conditional flow, instead of exception handling).

但就像 Yuck 所说的那样,我不推荐这个。您应该退后一步,了解您的设计以及您希望实现的目标。有一个更好的方法来做到这一点(即使用正常的条件流,而不是异常处理)。

回答by mdm

Yes, if you throwthe exception that you intend to catchfrom within the try, it will be caught in the catch section.

是的,如果您throw打算catch在 try 中处理异常,它将在 catch 部分中捕获。

I have to ask you why you would want to do this though? Exception handling is not meant to be a substitute for control flow.

我不得不问你为什么要这样做?异常处理并不意味着要替代控制流。

回答by juergen d

You could throw an exception to force a catch

您可以抛出异常以强制捕获

throw new Exception(...);

回答by Chuck

why are you catching an exception? Why not just run the code in your "else" block? If you MUST do it that way, just throw a new exception

你为什么要捕捉异常?为什么不直接在“else”块中运行代码?如果你必须这样做,就抛出一个新的异常

throw new Exception();

回答by jug

Yes, you have to throw exception :

是的,你必须抛出异常:

  try
  {
    throw new Exception("hello");
  }
  catch (Exception)
  {

     //run some code here...
  }

回答by Mark Schultheiss

I think what you want is a finallyblock: http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.80).aspx

我认为你想要的是一个finally块:http: //msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.80).aspx

see this

看到这个

try
 {
     doSomething();
 }
catch
 {
     catchSomething();
     throw an error
 } 
finally
 {
     alwaysDoThis();
 }

This is different if/when you do this:

如果/当您这样做时,情况会有所不同:

try
 {
     doSomething(); 
 }
 catch
 {
     catchSomething(); 
     throw an error
 }
  alwaysDoThis();// will not run on error (in the catch) condition

the the this last instance, if an error occurs, the catch will execute but NOT the alwaysDoThis();. Of course you can still have multiple catchas always.

最后一个实例,如果发生错误,catch 将执行,但不会执行alwaysDoThis();. 当然,您仍然可以catch像往常一样拥有多个。

回答by Ian

As cadrel said, but pass through an Exception to provide more feedback, which will be shown in the innerException:

正如cadrel所说,但是通过一个Exception来提供更多的反馈,会在innerException中显示:

try
{
    if (AnyConditionTrue)
    {
        MethodWhenTrue();
    }
    else
    {
        HandleError(new Exception("AnyCondition is not true"));
    }
}
catch (Exception ex)
{
    HandleError(ex);
}

...

...

private void HandleError(Exception ex) {
    throw new ApplicationException("Failure!", ex);
}

回答by Marinha do Nascimento

if(conditiontrue)
{

}
else{
    throw new Exception();
}

回答by Muzafar Hasan

public class CustomException: Exception
{
     public CustomException(string message)
        : base(message) { }

}

//

//

if(something == anything)
{
   throw new CustomException(" custom text message");
}

you can try this

你可以试试这个