.net 如何确定异常从何而来?
以下是代码结构:.net 如何确定异常从何而来?,.net,exception,.net,Exception,以下是代码结构: try { --outter code block; try { --inner code block; } catch(Exeption ex) { throw new Exception() --inner catch block } } catch(Exeption
try
{
--outter code block;
try
{
--inner code block;
}
catch(Exeption ex)
{
throw new Exception()
--inner catch block
}
}
catch(Exeption ex)
{
--outter catch block
}
如何在outter catch块中确定是在outter代码块中生成的异常还是在内部catch块中抛出的异常?抛出不同类型的异常或将不同的字符串参数传递给构造函数。抛出不同类型的异常或将不同的字符串参数传递给构造函数。方法如果您计划抛出新异常或仅使用
throw,则内部代码块应在其innerException
参数中包含execption
以使异常冒泡
try
{
//outter code block;
try
{
//inner code block;
}
catch(Exeption ex)
{
throw new MySpecialException("Some Extra Information", ex);
// or
throw;
}
}
catch(Exeption ex)
{
//ex.InnerException contains the "ex" from up above if you used MySpecialException
// or ex will be the same exception with the same stack trace if you used throw;
}
注意,执行
抛出新异常被认为是非常糟糕的做法,您应该总是抛出一些更派生的异常,或者您抛出的异常应该添加一些新的信息。如果不需要创建新异常来添加更多信息,只需使用抛出
如果您计划抛出新异常或仅使用抛出,则内部代码块应在其innerException
参数中包含execption
以使异常冒泡
try
{
//outter code block;
try
{
//inner code block;
}
catch(Exeption ex)
{
throw new MySpecialException("Some Extra Information", ex);
// or
throw;
}
}
catch(Exeption ex)
{
//ex.InnerException contains the "ex" from up above if you used MySpecialException
// or ex will be the same exception with the same stack trace if you used throw;
}
注意,执行抛出新异常被认为是非常糟糕的做法,您应该总是抛出一些更派生的异常,或者您抛出的异常应该添加一些新的信息。如果不需要创建新异常来添加更多信息,只需使用抛出代码>事实上,对于适当的不同类型,您不需要在catch块中使用此类代码。您的意思是如果“ex.message”具有字符串,则签入外部catch块?确实,对于适当的不同类型,在catch块中不需要这样的代码。你的意思是如果'ex.message'有字符串,就签入外部catch块吗?你真的有catch(Exeption ex)
作为异常处理程序的代码吗?你不应该那样做。这和使用goto
一样糟糕。你真的有catch(Exeption ex)
作为异常处理程序的代码吗?你不应该那样做。这和使用goto
一样糟糕。