2017-01-23 42 views
0

我想處理應用程序級別的任何Oracle Db異常。因此在Global.asax的Application_Error函數中編寫了下面的一段代碼如何檢查異常是否是Oracle異常?

代碼似乎更具體,但我想爲任何類型的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類嗎?

+0

但如何?我的意思是我想寫它的任何Oracle錯誤.. – SantyEssac

回答

0

可以檢查的異常是OracleException使用isas

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

或者:

OracleException oex = lastException as OracleException; 

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

但我不能在這裏添加OracleException dll – SantyEssac

+0

如何添加OracleException類的引用? – SantyEssac

+0

爲什麼不能???????? –

相關問題