【问题标题】:How to check if the exception is an Oracle exception?如何检查异常是否为 Oracle 异常?
【发布时间】:2017-01-23 14:16:46
【问题描述】:

我想在应用程序级别处理任何 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 类吗?

【问题讨论】:

  • 但是如何?我的意思是我想为任何 Oracle 错误写它..

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


【解决方案1】:

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

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

或者:

OracleException oex = lastException as OracleException;

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

【讨论】:

  • 但我无法在此处添加 OracleException dll
  • 如何添加 OracleException 类的引用?
猜你喜欢
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 2012-06-15
相关资源
最近更新 更多