Kel*_*all 11 c# exception-handling infinity dividebyzeroexception
我在VS2015 C#interactive中运行了以下代码片段并得到了一些非常奇怪的行为.
> double divide(double a, double b)
. {
. try
. {
. return a / b;
. }
. catch (DivideByZeroException exception)
. {
. throw new ArgumentException("Argument b must be non zero.", exception);
. }
. }
> divide(3,0)
Infinity
> 3 / 0
(1,1): error CS0020: Division by constant zero
> var b = 0;
> 3 / b
Attempted to divide by zero.
>
Run Code Online (Sandbox Code Playgroud)
为什么该方法返回无穷大而3/0引发错误并且3/b抛出格式化错误?我可以强制分裂抛出错误而不是返回无穷大吗?
如果我重新格式化方法
double divide(double a, double b)
{
if ( b == 0 )
{
throw new ArgumentException("Argument b must be non zero.", new DivideByZeroException());
}
return a / b;
}
Run Code Online (Sandbox Code Playgroud)
新的DivideByZeroException是否包含捕获的异常所具有的所有相同信息和结构?