I'm using DelegateCommands (Prism) inside my ViewModels which I expose to the outside as ICommands.
The caviat is: DelegateCommand.Execute is implemented as Task Execute(...) whereas ICommand.Execute is implemented as simple void Execute(...).
I noticed this, because exceptions were swallowed in the execute handler. While it is a typical behaviour for asyncs which are not awaited I did not expect this to occur for ICommand.Execute (which has no sign of being async).
If I execute the ICommand I will not be able to catch eventually thrown exceptions by the DelegateCommand since DelegateCommands Execute() method is async whereas ICommands is not.
Is there any way to catch the thrown exception when using the DelegateCommand as ICommand?
[Test]
public void DelegateToICommandExecute()
{
var dCommand = new DelegateCommand(() => { throw new Exception(); });
ICommand command = dCommand;
command.Execute(null); // Doesn't fail due to exception
}
Making the nUnit test case as async works, but visual studio complains that I have an async method without await anything as await ICommand.Execute is not possible.
Casting it explicitly to DelegateCommand would be possible but this would fix only the unit test and not the application's behaviour when an exception is thrown.
How should an application working with ICommand deal with async underlying calls swallowing the exception?
DelegateBase (from which DelegateCommand inherits) defines its Execute as async void Execute and then awaits its own Task Execute() call).
So while calling ICommand.Execute I end up effectively calling an async void under the hood.
ICommandoutside of yourViewModel? Something likeIAsyncCommandwhich inherits fromICommand? - Michael