无法在 catch 块中抛出异常

石子津

我有一个课程,不应该向最终用户显示任何对话框。如果该用户传递了错误的文件路径,我尝试抛出异常并在适当的类中处理它。但是,尽管有“throw”指令,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();
        }
    }
索蒂里斯·库基奥斯-帕诺普洛斯

我发现您的 Click 事件处理程序有问题。您将其定义为异步,但您没有在等待任何东西。

您应该更改 ReadGuyAsync 方法以返回 Task 而不是 void,如下所示:

public async Task ReadGuyAsync()

在 loadGuy_Click 方法中,你应该等待它:

await guyManager.ReadGuyAync();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法访问 PSQLException 的 catch 块。这个异常永远不会从 try 语句体中抛出

无法在 catch 块中捕获异常 - Yii2

C#-返回后在catch块中抛出异常

从 catch 块抛出多个异常的最佳方法

在 try 块中抛出异常后,catch 块不会立即执行

是否可以从具有嵌套异常的catch块中抛出异常?

catch块中的异常对象

laravel无法处理异常,catch块未执行

UnhandledPromiseRejectionWarning:错误抛出到没有catch块的异步函数内部,promise无法通过.catch()处理

在Javascript中,即使从不抛出异常,使用try-catch块是否昂贵?

为什么从 finally 块抛出的异常会忽略从 catch 块抛出的异常?

如果catch和finally块都抛出异常会怎样?

最后抛出异常。CLR行为与try-catch块

无法使用promises'catch'捕获在promise中抛出的错误

使用现有的 try/catch 块或重复的 catch 块而不抛出异常

try catch 块中的 xunit 核心无法正常工作

无法在try catch块中捕获Webservice调用方法

区分CATCH块中的异常与失败[RAKU]

try / catch块中未处理的异常

在catch / finally块中引发吞咽异常

在 try/catch 块中未捕获异常

在 try catch 块中重试异常

异常未捕获在try catch块中

Findbugs做出的贡献:方法从没有历史记录的catch块中抛出替代异常

无法在 try-catch C# 中捕获异常

React-无法在.map中使用Try Catch块(从try catch中返回jsx)

在等待的 Promise 中抛出的错误未在 catch 块中捕获

无法访问 Javascript async/await try/catch 客户端代码中的 res.statusMessage 以读取从快速后端抛出的异常

无法捕获 then 块中抛出的错误