我试图处理ReactiveUI(7.4.0.0)命令中抛出的异常,但是在Visual Studio的“输出”窗口中,所有东西似乎都被放在了内部并且永远不会出现。 我的测试代码是这样的:
class Program
{
static void Main(string[] args)
{
try
{
var ata = ReactiveCommand.CreateFromTask(async () => await AsyncTaskThrowException());
ata.ThrownExceptions.Subscribe(ex => Console.WriteLine($"async with await:\t{ex.Message}"));
ata.Execute();
// Exception thrown: 'System.InvalidOperationException' in System.Reactive.Windows.Threading.dll
// Exception thrown: 'System.Exception' in Test.exe
// Exception thrown: 'System.Exception' in mscorlib.dll
var atna = ReactiveCommand.CreateFromTask(() => AsyncTaskThrowException(),null, RxApp.MainThreadScheduler);
atna.ThrownExceptions.Subscribe(ex => Console.WriteLine($"async without await:\t{ex.Message}"));
atna.Execute();
// Exception thrown: 'System.InvalidOperationException' in System.Reactive.Windows.Threading.dll
// Exception thrown: 'System.Exception' in Test.exe
var ta = ReactiveCommand.CreateFromTask(async () => await TaskThrowException());
ta.ThrownExceptions.Subscribe(ex => Console.WriteLine($"async without await:\t{ex.Message}"));
ta.Execute();
// Exception thrown: 'System.InvalidOperationException' in System.Reactive.Windows.Threading.dll
// Exception thrown: 'System.Exception' in Test.exe
var tna = ReactiveCommand.CreateFromTask(() => TaskThrowException());
tna.ThrownExceptions.Subscribe(ex => Console.WriteLine($"async without await:\t{ex.Message}"));
tna.Execute();
// Exception thrown: 'System.InvalidOperationException' in System.Reactive.Windows.Threading.dll
// Exception thrown: 'System.Exception' in Test.exe
// Exception thrown: 'System.InvalidOperationException' in System.Reactive.Windows.Threading.dll
var sf = ReactiveCommand.Create(() => ThrowException());
sf.ThrownExceptions.Subscribe(ex => Console.WriteLine($"sync:\t{ex.Message}"));
sf.Execute();
// Exception thrown: 'System.InvalidOperationException' in System.Reactive.Windows.Threading.dll
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(ex.Message)}: {ex.Message}");
Debug.WriteLine($"{nameof(ex.StackTrace)}: {ex.StackTrace}");
}
Console.ReadLine();
}
static async Task<string> AsyncTaskThrowException()
{
await Task.Delay(100);
throw new Exception("Exception in async Task");
}
static Task<string> TaskThrowException()
{
throw new Exception("Exception in non-async Task");
}
static string ThrowException()
{
throw new Exception("Exception in sync func");
}
}
如果我评论除了Exception
之外的所有内容(包含有意的一个),则每次通话都会引发.Execute()
。
唯一打印的东西是第三个:
ReactiveCommand.CreateFromTask(() => TaskThrowException());
但仍然会抛出一些东西。
您能否帮助我理解为什么其他Exceptions
没有被ThrownExceptions
用途,以及如何完全处理错误以便他们不会在“输出”窗口中登录?
谢谢!