Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# “C”;如果抛出异常…“;_C#_Exception_Coding Style_Conditional Statements - Fatal编程技术网

C# “C”;如果抛出异常…“;

C# “C”;如果抛出异常…“;,c#,exception,coding-style,conditional-statements,C#,Exception,Coding Style,Conditional Statements,不确定这是否可能,或者可能被认为是不好的做法,但我想知道是否有一种很好的方法可以用C#编写上述if语句 类似于 if (method throws exception) { // do something } else { // do something else } 只使用普通的try-catch块。如果抛出异常,它将转到catch块,如果没有,它将继续执行可能抛出异常的方法之后的行 try { MethodThatMightThrowException() // do

不确定这是否可能,或者可能被认为是不好的做法,但我想知道是否有一种很好的方法可以用C#编写上述
if
语句

类似于

if (method throws exception)
{
    // do something
}
else
{
    // do something else
}

只使用普通的try-catch块。如果抛出异常,它将转到catch块,如果没有,它将继续执行可能抛出异常的方法之后的行

try
 {
 MethodThatMightThrowException()
 // do something else
 }
catch
 {
 // do something
 }

因此,您需要的是一个try-catch语句。这种结构在许多语言中都很常见,但对于C语言来说,它非常棒。 我将向您介绍Microsoft关于c#错误处理的文档。

这应该教会你你需要知道的一切。 以我的术语简单介绍一下它是如何工作的:

try {
//execute code that you expect to throw an exception
}
Catch (KindOfError ErrorVariable) {
//Error has been thrown, execute code for it.
msgbox.show("Error Raised: " + ErrorVariable.Code.ToString())
}
Finally {
//Execute code here you want to run regardless of error outcome
msgbox.show("This code runs with or without an exception being thrown")
}

这应该能帮到你

技术上可以捕获异常:

 try { 
   var result = DoSomeAction(arguments);

   /* do something else : result is a valid value*/
 }
 catch (SomeException) {   //TODO: put the right exception type
   /* If exception is thrown; result is not valid */ 

   // throw; // uncomment, if you want to rethrow the exception
 }
但是,您可以实现
TryGet
模式,并在以下情况下具有清晰的

并将其用作

 if (TryDoSomeAction(arguments, out var result)) {
   /* do something else : result is a valid value*/
 }
 else {
   /* result is not valid; if "exception" is thrown */ 
 }

您可以利用委托并创建一些静态辅助对象

在这种情况下,可以使用
Action
Func
。如果需要从已执行的函数返回一些值,请添加另一个接受
Func
的扩展方法

public static class SilentRunner
{
    public static void Run(Action action, Action<Exception> onErrorHandler)
    {
        try
        {
            action();
        }
        catch (Exception e)
        {
            onErrorHandler(e);
        }
    }

    public static T Run<T>(Func<T> func, Action<Exception> onErrorHandler)
    {
        try
        {
            return func();
        }
        catch (Exception e)
        {
            onErrorHandler(e);
        }

        return default(T);
    }
}
如果是
Func
,您也可以获取结果:

var result = SilentRunner.Run(
     () => DoSomething(someObject),
     ex => DoSomethingElse(someObject, ex));

最佳实践是将代码放在try-catch语句中。执行如下操作:`` try{A=Method();B=Method2();}catch(Exception){//Exception is thrown}``尝试中的所有内容都将继续执行,除非抛出一个异常。仍然不清楚。这里什么是
方法
?您可以
捕获
异常(并且
抛出;
如果需要,请再次执行):
尝试{…/*执行其他操作*/}catch(异常e){/*如果抛出异常*/throw;}
以及上面的注释,也许也可以略读一下?您正在寻找
TryGet
模式吗?
var result = SilentRunner.Run(
     () => DoSomething(someObject),
     ex => DoSomethingElse(someObject, ex));