I have a simple program:
class Program
{
static void Main(string[] args)
{
Run().Wait();
}
private static async Task Run()
{
string someVariable = null;
someVariable.Replace(",", ".");
}
}
Run() method is intentionally designed to throw NullReferenceException. What bothers me is why exception is thrown on the line
Run.Wait()
instead of on
someVariable.Replace(",",".");
The actuall exception is available in InnerException - why is that? I lose debugging context, because exception is thrown outside of Run method.

If my program is synchronous:
class Program
{
static void Main(string[] args)
{
Run();
}
private static void Run()
{
string someVariable = null;
someVariable.Replace(",", ".");
}
}
exception is thrown on the correct line. Why async breaks this?
When you call Run.Wait(), the Run() method has thrown a null exception then Wait method will throw AggregateException. Btw you don't loss your context. If you click on [View Details] and view the StackTrace of InnerException of the current exception, you can found that the exception came from Run() method:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With