C
C#2y 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
khamas2y 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
khamas2y 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
khamas2y 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
khamas2y ago
oh nice to know
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
pass data onclick from one page to another page in asp nethi all, im trying to redirect a page from **Main Page** to **Sub Page** when i click an item on **MASP NET Core default DI-Container, how pass in constructor not-registered parameter? [Answered]I want to create a Model class using a DI container, services registered in the container are passedShould I return the whole created object in POST or just return the id of that object ?When I creating a post method I find myself repeat what I did in GetById Should I just return the Iasp net cshtml javascript filehi all, i have an asp net project (SimpleApp) and in a cshtml file of my **SimpleApp ** project i trChange Visual Studio suggetions with custom analysersHiya.. ohh discords new "forum" channels are super useful for this huh?. anyway. So I managed to impProgram won’t run with just .net runtime [Answered]So my program that I published and is targeting .net 6 won’t run when the pc has just .net 6 runtimeNullReferenceException Object reference not set to an instance of an object```cs List<Effects> effectsList = activePlayerEffects[targetPlayer] ?? new List<Effects>(); ``` someDbContext.SaveChangesAsync has no effect on the collection of changed objectsHi. I get products collection from dbContext, change some values and want to save it in the databaseWindowsIdentity claims don't include all AD attributes, how do I fix that?I'm using windows authentication and want to display a user's full name, however on inspection the ihow to create a jwt for an existing user in identityI am looking to create a new JWT and validate it... for using tiwht signalr. so i want to know how t