【发布时间】:2020-03-28 13:30:28
【问题描述】:
我有一个类,不应该向最终用户显示任何对话框。万一该用户传递了错误的文件路径,我尝试抛出异常并在适当的类中处理它。 但是,尽管有“抛出”指令,Visual Studio 仍会显示异常对话框并在其发生后中断应用程序(调试模式)。在发布模式下,应用程序在提供错误的文件路径后会崩溃。我做错了什么?
GuyManager.cs:
private IStorageFile latestGuyFile;
public IStorageFile LatestGuyFile { get { return latestGuyFile; } }
public string Path { get; set; }
public async void ReadGuyAsync()
{
if (String.IsNullOrWhiteSpace(Path))
return;
try
{
latestGuyFile = await StorageFile.GetFileFromPathAsync(Path);
}
catch (Exception ex)
{
Debug.WriteLine("Error occured: " +ex.Message);
Debug.WriteLine(ex.StackTrace);
throw;
}
MainPage.xml.cs:
private async void loadGuy_Click(object sender, RoutedEventArgs e)
{
try
{
guyManager.ReadGuyAsync();
}
catch (Exception ex)
{
MessageDialog dialog = new MessageDialog("Error" + ex.Message);
await dialog.ShowAsync();
}
}
【问题讨论】:
标签: c# winforms xaml try-catch