0

我想处理应用程序级别的任何 Oracle Db 异常。因此在 Global.asax 的 Application_Error 函数中编写了以下代码

代码似乎更具体,但我想为任何类型的 Oracle 异常编写代码。

protected void Application_Error(object sender, EventArgs e)
{
    var isOracleException = false;
    //Get the error details.
    Exception lastException = Server.GetLastError();
    if (lastException != null)
    {  
        if (lastException.Message.StartsWith("ORA"))
        {
            isOracleException = true;      
        }
    }
    HttpContext httpContext = (sender as MvcApplication).Context;
    httpContext.ClearError();
    httpContext.Response.Clear();
    httpContext.Response.TrySkipIisCustomErrors = true;
    RouteData routeData = new RouteData();
    if (isOracleException)
    {
        routeData.Values["controller"] = "HandleError";
        routeData.Values["action"] = "Error";

        IController errorController = new HandleErrorController();
        var requestContext = new RequestContext(new HttpContextWrapper(httpContext), routeData);
        errorController.Execute(requestContext);
    }
}

我们可以访问 Global.asax 文件中的 OracleException 类吗?

4

1 回答 1

0

OracleException您可以使用isor来检查异常是否为as

if (lastException is OracleException)
{
    isOracleException = true;
    // or
    // do something
}

或者:

OracleException oex = lastException as OracleException;

if (oex != null)
{
    // do something with oex
}
于 2017-01-23T14:50:21.563 回答