asp.net - 在ASP.NET中 try catch 错误处理

标签 asp.net error-handling try-catch

我对 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/

相关文章:

javascript - 如何在选中 asp.net、c# 中 tabcontainer 中的复选框时启用附近(相应)文本框

asp.net - 无法在框架 3.5 中使用 requestvalidationmode

java - 在 Java catch block 中,您如何知道哪个方法/行抛出异常?

asp.net - 将网站从 ASP.NET 迁移到 ASP.NET MVC

c# - DataReader 已经打开

c# - 错误页面上的 ASP.net 捕获错误

php - 引用 - 这个错误在 PHP 中意味着什么?

error-handling - 快速错误处理 - 确定错误类型

java - 指定抛出 IOException

python - 如果函数为 false,则尝试/异常(exception)