C#
C#

help

Root Question Message

flkXI
flkXI8/23/2022
catching an exception in an async Task [Answered]

I can't seem to catch an exception that is thrown in an async Task, I put try/catch within the async task, when calling the task I put 'ConfigureAwait(true)', and I await it, I also have a catch in the parent method
flkXI
flkXI8/23/2022
try
{
    Refund refund = null;
    switch (frameworkEnum)
    {
        case PaymentFramework.Invoice:
            var invoice = _invoiceService.Get(bill.StripeInvoiceId);
                            
            //method exits on next line
            refund = _refundService.Create(new()
            {
                Charge = invoice.ChargeId,
                Reason = $"Seller chose to refund order {order.Id}"
            });
        break;
        case PaymentFramework.Intents:
            // doesn't matter
        break;
        default:
        break;
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}
flkXI
flkXI8/23/2022
method exits when I try to create a refund in stripe
flkXI
flkXI8/23/2022
but nothing gets caught
RazorSharpFang
RazorSharpFang8/23/2022
You're not awaiting anything?
If you're not awaiting it, execution may occur asynchronously on a different thread after the current method returns, if any of those calls (e.g.: Get(...) or Create(...) ) are asynchronous calls that await other async methods perhaps.
Are those methods async Task methods?
flkXI
flkXI8/23/2022
this is the body of the awaited method
flkXI
flkXI8/23/2022
idk why it throws now, maybe a VS bug
RazorSharpFang
RazorSharpFang8/23/2022
What exception does it even throw?
RazorSharpFang
RazorSharpFang8/23/2022
Are you seeing it in the exception break?
RazorSharpFang
RazorSharpFang8/23/2022
Can you screenshot the call-stack of that exception?
RazorSharpFang
RazorSharpFang8/23/2022
Are you also absolutely sure that it's not caught internally by the methods?
flkXI
flkXI8/23/2022
no the method I call it from used to just exit, I rebuilt and now I can catch the exception, it's a StripeException, problem solved
flkXI
flkXI8/23/2022
just rebuilt it
flkXI
flkXI8/23/2022
thanks for the help
RazorSharpFang
RazorSharpFang8/23/2022
Okay, weird that.
Use command /close to close the post if you're happy to close it.
flkXI
flkXI8/23/2022
oh nice to know
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy