C
C#3y ago
khamas

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
8 Replies
khamas
khamasOP3y ago
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);
}
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);
}
method exits when I try to create a refund in stripe but nothing gets caught
RazorSharpFang
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?
khamas
khamasOP3y ago
this is the body of the awaited method idk why it throws now, maybe a VS bug
RazorSharpFang
What exception does it even throw? Are you seeing it in the exception break? Can you screenshot the call-stack of that exception? Are you also absolutely sure that it's not caught internally by the methods?
khamas
khamasOP3y ago
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 just rebuilt it thanks for the help
RazorSharpFang
Okay, weird that. Use command /close to close the post if you're happy to close it.
khamas
khamasOP3y ago
oh nice to know
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server