我对 Try-Catch 块有一个疑问。
下面是我的代码
private void PopulateDDL()
{
try
{
if (my condition)
{
code
}
else
{
throw new Exception(ErrorMessage);
}
}
catch (Exception ex)
{
logerror(ex);
}
}
如果以下代码出错,将执行哪个捕获块
其他
{
throw new Exception(ErrorMessage);
}
最佳答案
从MSDN:
When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception. If the currently executing method does not contain such a catch block, the CLR looks at the method that called the current method, and so on up the call stack. If no catch block is found, then the CLR displays an unhandled exception message to the user and stops execution of the program.
您已定义的catch块:
catch (Exception ex)
{
RaiseWebError(ex);
}
将首先针对
throw new Exception(ErrorMessage);
异常执行如果
RaiseWebError
重新抛出该异常,则将由调用堆栈中的下一个try-catch块(即您引用的父方法)处理该异常。但是,如果RaiseWebError
以某种方式(也许通过记录异常)处理了异常,则在第一次try-catch之后将继续执行。
关于asp.net - 在ASP.NET中 try catch 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22166106/