如何检查异常是否是Oracle异常?

时间:2017-01-23 14:16:47

标签: c# asp.net-mvc-4 exception-handling oracleexception

我想在应用程序级别上处理任何Oracle Db异常。在Global.asax的Application_Error函数中的一段代码后面编写的Hence

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

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类吗?

1 个答案:

答案 0 :(得分:0)

您可以使用OracleExceptionis检查例外是否为as

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

或者:

OracleException oex = lastException as OracleException;

if (oex != null)
{
    // do something with oex
}