0

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: enter image description here

Yura Babiy
  • 515
  • 7
  • 22
  • What exactly is the question? Where do you want to see the exception and when? – Haukinger Nov 19 '16 at 09:51
  • The question is: What I do wrong and what I should fix to have normal exceptions, that throw a message. – Yura Babiy Nov 19 '16 at 11:28
  • I guess Stephen's the best one to comment on his own classes, but from a glance at his code it looks like NotifyTask.Create() effectively creates a running task which will notify when it's complete; it _does not wait for it to complete_. Is that what you're expecting? – sellotape Nov 19 '16 at 15:51
  • Then this "plain async/await and it was throwing me an aggregate exception" seems odd - async/await should generally not throw an AggregateException. Are you sure you were using only async/await before and not .Result/.Wait()? – sellotape Nov 19 '16 at 15:54
  • @sellotape Yes, you right about Stephen's. About aggregate exception, sorry, maybe I was wrong, it was a bit far from when I saw it, so maybe my thoughts mixed. Look at updated question. – Yura Babiy Nov 19 '16 at 17:06

1 Answers1

4

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, e.g., 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.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Hello, Stephen. Thank you that you took a look at my question. I really respect you, that you do not just give answer, but you also trying to give your best of whole explanation. It is really what I need because of my little skill. About exceptions: I tried few times just now, and it still what I say. If I put throw exception outside try/catch, then it left ViewModel and works just fine, without any window of exception. If it is on the block, then it catches, but exception object is really null and if I throw ex. here, it just ignores and lefts ViewModel and work fine. – Yura Babiy Nov 19 '16 at 17:37
  • @YuraBabiy: To my knowledge, it is not possible for a `catch (T ex)` block to ever set `ex` to `null`. Can you post a minimal, reproducible example? – Stephen Cleary Nov 19 '16 at 18:45
  • As @StephenCleary says, catching a null exception should never happen. I notice [this](http://stackoverflow.com/questions/5634417/caught-exception-is-null-itself), though; perhaps your code and environment fall into that rare case? BTW, normally there is no issue naming all the exceptions you catch "ex". – sellotape Nov 19 '16 at 19:04
  • @StephenCleary I don't know why I didn't try it, but it was my bad, that exception was null, because it wasn't, just I checked it on the catch line, where it was not set yet. But exceptions are still ignored. Maybe you came to some new thoughts with that? The example I will try to produce the only tomorrow if you still needs it. – Yura Babiy Nov 19 '16 at 19:24
  • @sellotape look at my comment to Stephen – Yura Babiy Nov 19 '16 at 19:24
  • @YuraBabiy: I am completely lost with what you mean. You catch the exception, OK. If you don't want the exception to be ignored, then don't ignore it. – Stephen Cleary Nov 20 '16 at 00:28
  • @StephenCleary With all these my wrong conclusions, me scary to say something for sure, but what I see now, that they are ignored without my hand. – Yura Babiy Nov 20 '16 at 05:57