C# 从递归中获取异常
我有一个递归方法:C# 从递归中获取异常,c#,exception,recursion,C#,Exception,Recursion,我有一个递归方法: private static int F(Tree tree) 有一个try块: try { ... return x; } catch (DivideByZeroException) { Console.WriteLine("Divide Error"); throw; } 我的问题是如何从rucursion获得?因为如果我除以零,我捕捉到异常,但仍然得到错误。 谢谢。如果在catch中再次抛出异常,如果没有再次捕获,则程序将崩溃,这是在
private static int F(Tree tree)
有一个try块:
try
{
...
return x;
}
catch (DivideByZeroException)
{
Console.WriteLine("Divide Error");
throw;
}
我的问题是如何从rucursion获得?因为如果我除以零,我捕捉到异常,但仍然得到错误。
谢谢。如果在
catch
中再次抛出异常,如果没有再次捕获,则程序将崩溃,这是在这种情况下的预期行为
是抛出的同一个异常,还是您在
catch
中捕获的异常?如果您希望代码能够在异常发生后继续,则必须包装抛出错误的单个操作,然后编写处理该错误的代码,例如,通过设置某个变量来表示推断值。如果有多个操作,则必须包装每个操作。例如:
double a;
double b;
try
{
a = 1000 / 0 ; //Some operation that computes a, but might divide by zero
}
catch (DivideByZeroException)
{
a = Double.PositiveInfinity; //The desired value of a when divide by zero occurs
}
try
{
b = -1000/0; //Some operation that computes b, but might divide by zero
}
catch (DivideByZeroException)
{
b = Double.NegativeInfinity; //The desired value of b when divide by zero occurs
}
如果删除
throw
并替换为返回0代码>?