【发布时间】:2014-08-27 03:45:27
【问题描述】:
当我在代码中遇到异常时,它总是会抛出并冒泡回到 app.g.i.cs 文件。如果我将导致异常的方法包装在 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;
}
}
每次我点击 DeleteAsync 方法,并且文件不存在时,我都希望抛出并吞下一个异常。相反,我的渔获永远不会被击中。它一直冒泡到 app.g.i 文件。
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
}
我应该注意以下几点:
try
{
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch (Exception)
{
return;
}
结果相同,catch 永远不会被击中。
谁能告诉我为什么我的异常处理程序(不仅仅是这个,而是我的应用程序中的每个)都没有被击中?如果我的异常处理程序从未有机会处理它们,那么正确处理异常真的很困难。该应用程序被编写为通用 Windows 8.1/Windows Phone 8.1 应用程序。
我提供了完整的异常详细信息,但我的问题并不是导致异常的真正原因,而是为什么我的捕获(即使我只是使用 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}
谢谢!
【问题讨论】:
-
你内心的异常是什么?
-
内部异常为空
-
我用异常更新了我的 OP,虽然我的问题不是具体是什么导致了异常(我知道是什么导致的);而是为什么我的
try/catch没有抓住它。
标签: c# excel exception-handling windows-runtime