简体   繁体   中英

Exceptions are just ignored in async code block

Before I use Nito.MVVM, I used plain async/await and it was throwing me an aggregate exception and I could read into it and know what I have. But since Nito, my exceptions are ignored and the program jumps from async code block and continue executes. I know that it catch exceptions because when I put a breakpoint on catch(Exception ex) line it breaks here but with ex = null . I know that NotifyTask has properties to check if an exception was thrown but where I put it, it checks when Task is uncompleted, not when I need it.

View model:

public FileExplorerPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
            _manager = new FileExplorerManager();

            Files = NotifyTask.Create(GetFilesAsync("UniorDev", "GitRemote/GitRemote"));

        }

Private method:

private async Task<ObservableCollection<FileExplorerModel>> GetFilesAsync(string login, string reposName)
        {
            return new ObservableCollection<FileExplorerModel>(await _manager.GetFilesAsync(login, reposName));
        }

Manager method(where exception throws):

 public async Task<List<FileExplorerModel>> GetFilesAsync(string login, string reposName)
        {
            //try
            //{
                var gitHubFiles = await GetGitHubFilesAsync(login, reposName);

                var gitRemoteFiles = new List<FileExplorerModel>();

                foreach ( var file in gitHubFiles )
                {
                    if ( file.Type == ContentType.Symlink || file.Type == ContentType.Submodule ) continue;

                    var model = new FileExplorerModel
                    {
                        Name = file.Name,
                        FileType = file.Type.ToString()
                    };

                    if ( model.IsFolder )
                    {
                        var nextFiles = await GetGitHubFilesAsync(login, reposName);
                        var count = nextFiles.Count;
                    }

                    model.FileSize = file.Size.ToString();

                    gitRemoteFiles.Add(model);
                }

                return gitRemoteFiles;
            //}
            //catch ( WebException ex )
            //{
            //    throw new Exception("Something wrong with internet connection, try to On Internet " + ex.Message);
            //}
            //catch ( Exception ex )
            //{
            //    throw new Exception("Getting ExplorerFiles from github failed! " + ex.Message);
            //}
        }

With try/catch or without it has the same effect. This behavior is anywhere where I have NotifyTask.

Update There is no event, that fires when exception occurred, but there is Property Changed event, so I used it and added this code:

private void FilesOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            throw new Exception("EXCEPTION");
            bool failed;
            if ( Files.IsFaulted )
                failed = true;
        } 

And exception not fires. I added throw exception in App class (main class) and it fired. And when I have exceptions that come from XAML, it also fires. So maybe it not fires when it comes from a view model, or something else. I have no idea. Will be very happy for some help with it. Update

We deal with exception = null, but the question is still alive. What I wanna add, that I rarely this issue, when the app is starting to launch on the physic device. I read some info about it, and it doesn't seem to be related, but maybe it is: 在此处输入图片说明

I'm not entirely sure what your desired behavior is, but here's some information I hope you find useful.

NotifyTask is a data-bindable wrapper around Task . That's really all it does. So, if its Task faults with an exception, then it will update its own data-bindable properties regarding that exception. NotifyTask is intended for use when you want the UI to respond to a task completing, eg, show a spinner while the task is in progress, an error message if the task faults, and data if the task completes successfully.

If you want your application to respond to the task faulting (with code, not just a UI update), then you should use try / catch like you have commented out in GetFilesAsync . NotifyTask doesn't change how those exceptions work; they should work just fine.

I know that it catch exceptions because when I put a breakpoint on catch(Exception ex) line it breaks here but with ex = null.

That's not possible. I suggest you try it again.

I know that NotifyTask has properties to check if an exception was thrown but where I put it, it checks when Task is uncompleted, not when I need it.

If you really want to (asynchronously) wait for the task to complete and then check for exceptions, then you can do so like this:

await Files.TaskCompleted;
var ex = Files.InnerException;

Or, if you just want to re-raise the exception:

await Files.Task;

Though I must say this usage is extremely unusual. The much more proper thing to do is to have a try / catch within your GetFilesAsync .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM