gpt4 book ai didi

c# - 何时在 c# 中重新抛出异常?

转载 作者:行者123 更新时间:2023-12-04 10:18:10 24 4
gpt4 key购买 nike

我已经阅读了为什么我们需要抛出异常并重新抛出它。但是我对何时重新抛出异常感到困惑?我在 CalculationOperationNotSupportedException catch 中放入 throw 时添加了一个示例,之后,我将 Stack Trace 与 Rethrowing 和不 Rethrowing 进行了比较。 99% 都是一样的,但是当您重新抛出异常时,它只会添加位置。
当然,如果你准确地进行了两次堆栈跟踪。第 35 行是“抛出”位置编号,第 28 行是 int result =calculator.Calculate(number1, number2, operation);
我认为 Stack Trace 不在这里重新抛出更好。你怎么看?

Stack Trace without rethrowing(throw) 我评论了它。

at ConsoleCalculator.Calculator.Calculate(Int32 number1, Int32 number2, String operation) in C:\Users\Behnam\Desktop\c-sharp-error-handling-exceptions\06\demos\after\03UsingExceptions\ConsoleCalculator\Calculator.cs:line 25 at ConsoleCalculator.Program.Main(String[] args) in C:\Users\Behnam\Desktop\c-sharp-error-handling-exceptions\06\demos\after\03UsingExceptions\ConsoleCalculator\Program.cs:line 28



在 catch 中重新抛出的堆栈跟踪 (CalculationOperationNotSupportedException ex)

at ConsoleCalculator.Calculator.Calculate(Int32 number1, Int32 number2, String operation) in C:\Users\Behnam\Desktop\c-sharp-error-handling-exceptions\06\demos\after\03UsingExceptions\ConsoleCalculator\Calculator.cs:line 25 at ConsoleCalculator.Program.Main(String[] args) in C:\Users\Behnam\Desktop\c-sharp-error-handling-exceptions\06\demos\after\03UsingExceptions\ConsoleCalculator\Program.cs:line 35


public int Calculate(int number1, int number2, string operation)
{
string nonNullOperation =
operation ?? throw new ArgumentNullException(nameof(operation));

if (nonNullOperation == "/")
{
try
{
return Divide(number1, number2);
}
catch (ArithmeticException ex)
{
throw new CalculationException("An error occurred during division", ex);
}
}
else
{
throw new CalculationOperationNotSupportedException(operation);
}
}
static void Main(string[] args)
{
var calculator = new Calculator();
int number1=1;
int number2=1;
string operation = "+";
try
{
int result = calculator.Calculate(number1, number2, operation);
DisplayResult(result);
}
catch (CalculationOperationNotSupportedException ex)
{
// Log.Error(ex);
WriteLine(ex);
throw;
}
}

最佳答案

有两篇关于我经常链接的主题的文章。我认为它们是必读的。

  • https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/
  • https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

  • 基本上,如果您无法处理异常,则不应捕获异常。但有时,您必须对任何有关 Exception 的规则进行异常(exception)处理(没有双关语意)。您可能需要更广泛地捕捉,然后将“捕捉和释放”应用于您获得的额外异常。例如,这是我尝试复制 TryParse:
    //Parse throws ArgumentNull, Format and Overflow Exceptions.
    //And they only have Exception as base class in common, but identical handling code (output = 0 and return false).

    bool TryParse(string input, out int output){
    try{
    output = int.Parse(input);
    }
    catch (Exception ex){
    if(ex is ArgumentNullException ||
    ex is FormatException ||
    ex is OverflowException){
    //these are the exceptions I am looking for. I will do my thing.
    output = 0;
    return false;
    }
    else{
    //Not the exceptions I expect. Best to just let them go on their way.
    throw;
    }
    }

    //I am pretty sure the Exception replaces the return value in exception case.
    //So this one will only be returned without any Exceptions, expected or unexpected
    return true;
    }

    关于c# - 何时在 c# 中重新抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60992432/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com