2011-03-15 13 views
0

私のWPFアプリケーションでは、LINQ to SQLを使用していますので、SubmitChanges()を呼び出すと例外がスローされますが、キャッチできません。ここでLINQ to SQLのSqlExceptionをキャッチする方法

は、関連するコードです:

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()を呼び出すコードです。


EDIT:

完全な例外はこれです:あなたが提供try-catchブロックがCreateNewCategory(スタックトレースを見て)メソッドと型の内側にある場合

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:Done ... – Nawaz

答えて

1

実際にSqlExceptionがスローされた例外の場合は、それをキャッチする必要があります。

また、アプリケーションですべての例外をキャッチする場合は、UIスレッドの例外の場合はApplication.DispatcherUnhandledExceptionイベントに、他のスレッドの場合はAppDomain.CurrentDomain.UnhandledExceptionにサブスクライブする必要があります。

+1

Application.DispatcherUnhandledExceptionとAppDomain.CurrentDomain.UnhandledExceptionは、エラーを記録して正常にクラッシュさせるためにのみ使用する必要があります。これらのイベントを使用して例外から回復しようとしないでください。 – Phil