Joh*_*ger 2 c# excel exception-handling windows-runtime
当我在代码中遇到异常时,它会一直抛出并冒泡回app.gics文件.如果我在try/catch中包装导致异常的方法并不重要,它仍然会回到App实例.
这是我尝试使用的方法:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
每次我点击DeleteAsync方法,并且该文件不存在,我希望抛出并吞下一个异常.相反,我的捕获永远不会受到打击.它一直冒泡到app.gi文件.
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
}
Run Code Online (Sandbox Code Playgroud)
我应该注意以下几点:
try
{
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch (Exception)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
具有相同的结果,捕获永远不会被击中.
有人可以告诉我为什么我的异常处理程序(不只是这个,但我的应用程序中的每一个)都没有被击中?如果我的异常处理程序从未被提供处理它们的机会,那么正确处理异常真的很困难.该应用程序是作为通用Windows 8.1/Windows Phone 8.1应用程序编写的.
我提供了完整的异常详细信息,但是我的问题并不是导致异常的原因,而是为什么我的catch(即使我只是使用Exception而不是FileNotFoundException)也没有被命中.
- 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}
Run Code Online (Sandbox Code Playgroud)
谢谢!
这个自动生成的代码非常不幸,常见的做法是在附加调试器时不订阅任何未处理的异常处理程序.因为这样可以防止调试器在出现问题时向您显示出现了什么问题.特别是在异步代码中很痛苦,因为抛出异常的代码在调用堆栈中是不可见的.然而,它似乎是必要的,没有那个处理程序,它的工作效果非常差.微软有一些工作要做,以使这更顺利.
你现在唯一真正的防御就是使用Debug + Exceptions,勾选CLR异常的Thrown复选框.这会强制调试器在抛出异常时停止.
当您再次运行代码时,您现在看到了真正的问题,它是抛出的GetFileAsync()方法.由于它不在try {}块中,因此您的catch子句无法吞下它.从技术上讲,这是你可以推断的东西,删除一个不存在的文件不是一个错误.但是,当然,从调试器获得帮助并没有伤害.固定:
var appdata = ApplicationData.Current;
try
{
StorageFile file = await appdata.LocalFolder.GetFileAsync("parseSession");
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch (FileNotFoundException)
{
// Okay now.
}
Run Code Online (Sandbox Code Playgroud)
这可以解决您的问题.我还没准备好像你想要的那样宣布这是一个普遍的问题,你可能只是错过了其他一些不在try {}块中的代码失败的情况.你原谅了,你没有从这些异常中获得良好的调试信息.