是否可以确定是否在没有catch块的情况下抛出了异常? Visual Studio能够从我的程序状态中挖掘异常并将其作为伪变量($ exception)。我可以做类似的事情吗?
我正在使用外部库,我无法在那里修改try-finally块。
以下是示例代码:
class Program
{
static void Main()
{
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
Console.WriteLine("UnobservedTaskException"); // this is never printed out
};
// <external>
// this is simplified code of referenced library, I can't change this
Task.Run(async () =>
{
try
{
BeforeExecute();
await Execute(); // the library uses reflection to get the proper method dynamically, this is just simplification
}
finally
{
AfterExecute();
}
});
// exception is never rethrown, because the library isn't (a)waiting the task here :(
// </external>
Console.ReadKey(true);
}
static void BeforeExecute()
{
// I use inheritance for this method, so I have to change it in just one place
Console.WriteLine("BeforeExecute");
}
static void AfterExecute()
{
// I use inheritance for this method, so I have to change it in just one place
Console.WriteLine("AfterExecute");
// >>> can I have knowledge of the exception here? <<<
}
static Task Execute()
{
// I have multiple methods like this one, modifying this would be too much work
Console.WriteLine("Execute");
throw new Exception(); // the exception ends in abyss :(
}
}