Unhandled Exception issue in WinRT
c#, excel, exception, windows-runtime
Solution
This auto-generated code is rather unfortunate, the common wisdom is to not subscribe any unhandled exception handler when a debugger is attached. Since that prevents the debugger from showing you what is going wrong at the moment it goes wrong. Especially painful in async code since the code that threw the exception is not visible on the call stack. It however appears to be necessary, it works very poorly without that handler. Microsoft has some work to do to make this smoother.
Your only real defense against this right now is to use Debug + Exceptions, tick the Thrown checkbox for CLR exceptions. This forces the debugger to stop when the exception is thrown.
When you run your code again, you now see the real problem, it is the GetFileAsync() method that throws. Since it is not in the try {} block, your catch clause cannot swallow it. Technically it is something you can reason out, deleting a non-existing file is not an error. But of course, getting help from the debugger doesn't hurt. Fix:
var appdata = ApplicationData.Current;
try
{
StorageFile file = await appdata.LocalFolder.GetFileAsync("parseSession");
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch (FileNotFoundException)
{
// Okay now.
}
This fixes your problem. I'm not ready to declare this a general problem like you want to, you might simply have missed some other cases of code failing that's not in a try {} block. You are forgiven, you are not getting good debug info from these exceptions.
Problem
When I encounter an exception within my code, it will always throw and bubble all the way back to the app.g.i.cs file. It does not matter if I wrap the method causing the exception in a try/catch, it still bubbles back to the App instance. This is the method I am trying to use: ``` public static async Task Clear() { userSessionToken = string.Empty; var appdata = ApplicationData.Current; StorageFile file = await appdata.LocalFolder.GetFileAsync("parseSession"); try { await file.DeleteAsync(StorageDeleteOption.PermanentDelete); } catch (FileNotFoundException) { return; } } ``` Each time I hit the DeleteAsync method, and the file does not exist, I expect an exception to be thrown and swallowed. Instead, my catch never gets hit. It bubbles all the way up to the app.g.i file. ``` public void InitializeComponent() { if (_contentLoaded) return; _contentLoaded = true; #if DEBUG && !DISABLE_XAML_GENERATED_BINDING_DEBUG_OUTPUT DebugSettings.BindingFailed += (sender, args) => { global::System.Diagnostics.Debug.WriteLine(args.Message); }; #endif #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION UnhandledException += (sender, e) => { // --> THIS Catches the exception <-- if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); }; #endif } ``` I should note that the following: ``` try { await file.DeleteAsync(StorageDeleteOption.PermanentDelete); } catch (Exception) { return; } ``` has the same result, the catch is never hit. Can someone please tell me why my exception handlers (it's not just this one, but each one in my app) are not being hit? It's really difficult to properly handle exceptions if my exception handlers are never being provided a chance to handle them. The app is wrote as a Universal Windows 8.1/Windows Phone 8.1 app. I'm providing the full exception details, however my question is not really what caused the exception, but rather why my catch (even if I just use Exception instead of FileNotFoundException) does not get hit. ``` - Exception {System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Actions.Services.ParseRest.ParseSession.<Clear>d__e.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Actions.Services.ParseRest.ParseRestUserService.<GetUserAsync>d__a.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Actions.Repositories.User.UserRepository.<GetUserAsync>d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Actions.Apps.WinRT.App.<OnLaunchApplication>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.Practices.Prism.Mvvm.MvvmAppBase.<OnLaunched>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state) at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()} System.Exception {System.IO.FileNotFoundException} ``` Thanks!