Tôi cố gắng nắm bắt ngoại lệ cơ sở dữ liệu khi máy chủ cơ sở dữ liệu bị hỏng. Chúng tôi sử dụng Sybase IAnywhere.Tại sao không thể bắt ngoại lệ bên trong?
Tôi thường xuyên sử dụng C# try catch để lấy tên của ngoại lệ cơ sở dữ liệu.
try
{
//code here
}
catch (Exception ex)
{
Logging.Log.logItem(LogType.Exception, "Exception in isDBRunning", "App_Startup::isDBRunning() ", "GetBaseException=" + ex.GetBaseException().ToString() + "\nMessage=" + ex.Message + "\nStackTrace: " + ex.StackTrace + "\nInnerException: " + ex.InnerException);
}
Ngoại lệ in ra là thế này:
GetBaseException=iAnywhere.Data.SQLAnywhere.SAException: Database server not found
at iAnywhere.Data.SQLAnywhere.SAConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
Message=The underlying provider failed on Open.
StackTrace: at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
at System.Data.EntityClient.EntityConnection.Open()
at System.Data.Objects.ObjectContext.EnsureConnection()
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at Analogic.SystemSoftware.App.isDBRunning() in C:\workspace\SystemSoftware\SystemSoftware\src\startup\App.xaml.cs:line 158
InnerException: iAnywhere.Data.SQLAnywhere.SAException: Database server not found
at iAnywhere.Data.SQLAnywhere.SAConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
Vì vậy, tôi nghĩ rằng iAnywhere.Data.SQLAnywhere.SAException
là ngoại lệ thật tôi nên xử lý. Sau đó, tôi đã thêm một nội dung bắt buộc cho nó:
try
{
//code here
}
catch (iAnywhere.Data.SQLAnywhere.SAException ex)
{
Logging.Log.logItem(LogType.Exception, "Exception in isDBRunning 1", "App_Startup::isDBRunning() ", "GetBaseException=" + ex.GetBaseException().ToString() + "\nMessage=" + ex.Message + "\nStackTrace: " + ex.StackTrace + "\nInnerException: " + ex.InnerException);
}
catch (Exception ex)
{
Logging.Log.logItem(LogType.Exception, "Exception in isDBRunning", "App_Startup::isDBRunning() ", "GetBaseException=" + ex.GetBaseException().ToString() + "\nMessage=" + ex.Message + "\nStackTrace: " + ex.StackTrace + "\nInnerException: " + ex.InnerException);
}
Nhưng không bị bắt gặp iAnywhere.Data.SQLAnywhere.SAException
. Tôi vẫn bị bắt số Exception
. tại sao?
Bạn cũng có thể mở rộng trên "// mã ở đây" không, đây có phải là mã quanh dòng 158 của tệp app.xaml.cs của bạn không? – NotMe
Ngoài ra, bạn có * chắc chắn * đây là mã được triển khai không? – NotMe
"// mã ở đây" có nghĩa là mã để kết nối với cơ sở dữ liệu để thực hiện truy xuất. Và tôi tắt máy chủ db. Vì vậy, tôi sẽ nhận được ngoại lệ từ cơ sở dữ liệu. – 5YrsLaterDBA