How to check if the exception is an Oracle exception?

834 Views Asked by At

I want to handle any Oracle Db Exception on Application level.Hence written following piece of code in Application_Error function of Global.asax

The code seems to be more specific but i want to write code for any kind of 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);
    }
}

Can we access OracleException Class in Global.asax file ?

1

There are 1 best solutions below

2
Patrick Hofman On

You can check if the exception is an OracleException by using is or as:

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

Or:

OracleException oex = lastException as OracleException;

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