2011-03-15 2 views
0

내 WPF 응용 프로그램에서 LINQ to SQL을 사용하고 있으므로 SubmitChanges()을 호출하면 예외가 발생하지만 catch 할 수 없습니다. 여기 LINQ to SQL에서 SqlException을 catch하는 방법

는 관련 코드 :

try 
{ 
    FeatureDataContext.Features.InsertOnSubmit(newFeature); 
    FeatureDataContext.SubmitChanges(); 
} 
catch (SqlException sqlEx) 
{ 
    System.Windows.MessageBox.Show("CreateNewFeature : " + sqlEx.Message); 
} 

그리고 이것은 위가 던지는 예외 :

Exception Info: System.Data.SqlClient.SqlException 
Stack: 
    at System.Data.Linq.DataContext.SubmitChanges(System.Data.Linq.ConflictMode) 
    at System.Data.Linq.DataContext.SubmitChanges() 
    at [....] 

어떻게 WPF 응용 프로그램에서 SqlException을 잡으려고?

나는 SubmitChanges()이 예외를 발생시키지 않는 것을 보았습니다. 그러나 예외 지점을 발생시킨 코드는 SubmitChanges()을 호출하는 코드입니다.


편집 :

Application: CompositeApplicationLauncher.exe 
Framework Version: v4.0.30319 
Description: The process was terminated due to an unhandled exception. 
Exception Info: System.Data.SqlClient.SqlException 
Stack: 
    at System.Data.Linq.DataContext.SubmitChanges(System.Data.Linq.ConflictMode) 
    at System.Data.Linq.DataContext.SubmitChanges() 
    at Runaware.Wpf.CompositeApplication.SessionReviewer.SessionDataProvider.CreateNewCategory(Runaware.InsightAnalysis.ApplicationFeatures.ObjectModel.ApplicationInfo) 
    at Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Execute(System.__Canon) 
    at Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].System.Windows.Input.ICommand.Execute(System.Object) 
    at Runaware.Wpf.CompositeApplication.Commands.DelegateExecute.ExecuteCaller(System.Object, System.Windows.RoutedEventArgs) 
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs) 
    at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean) 
    at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs) 
    at System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs) 
    at System.Windows.Controls.MenuItem.InvokeClickAfterRender(System.Object) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 
    at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
    at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) 
    at System.Threading.ExecutionContext.runTryCode(System.Object) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object) 
    at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
    at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
    at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
    at System.Windows.Threading.DispatcherOperation.Invoke() 
    at System.Windows.Threading.Dispatcher.ProcessQueue() 
    at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 
    at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 
    at System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) 
    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) 
    at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) 
    at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 
    at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) 
    at System.Windows.Threading.Dispatcher.Run() 
    at System.Windows.Application.RunDispatcher(System.Object) 
    at System.Windows.Application.RunInternal(System.Windows.Window) 
    at System.Windows.Application.Run(System.Windows.Window) 
    at System.Windows.Application.Run() 
    at Runware.Wpf.CompositeApplication.App.Main() 
+0

예외의 전체 스택 추적을 포함 할 수 있습니까? –

+0

@Pavlo : 완료 ... – Nawaz

답변

1

당신이 제공하는 시도-catch 블록이 (스택 트레이스보고)를 CreateNewCategory 내부에있는 경우, 방법 및 유형 :

전체 예외가 이것이다 실제로 던져 질 예외의 SqlException, 그때 잡아야한다.

또는 응용 프로그램에서 모든 예외를 catch하려면 UI 스레드에서 예외의 경우 Application.DispatcherUnhandledException 이벤트를 구독하고 다른 스레드에서는 예외의 경우 AppDomain.CurrentDomain.UnhandledException을 구독해야합니다.

+1

Application.DispatcherUnhandledException 및 AppDomain.CurrentDomain.UnhandledException은 오류를 기록하고 정상적으로 응용 프로그램을 충돌 시키도록 사용해야합니다. 이러한 이벤트를 사용하여 예외를 복구하려고 시도하지 마십시오. – Phil

관련 문제