question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to return -1 in catch block.

See original GitHub issue

I’m trying to catch the DbUpdateConcurrencyException and return -1:

var number = await _numberingManager.GetNumberByName(profileName).FirstOrDefaultAsync();
    try
    {
        if (number != null)
        {
            await Task.Delay(delay);
            NextNumber(number);
            await CurrentUnitOfWork.SaveChangesAsync();
            return number.LastUsedNumber;
        }
        else
        {
            throw new UserFriendlyException(string.Format("Record not found: {0}", profileName));
        }
    }
    catch (DbUpdateConcurrencyException ex)
    {
        return -1;
    }
    catch (Exception ex)
    {
        throw new UserFriendlyException(ex.Message);
    }

But I keep getting even I return -1:

{
  "result": null,
  "targetUrl": null,
  "success": false,
  "error": {
    "code": 0,
    "message": "An internal error occurred during your request!",
    "details": null,
    "validationErrors": null
  },
  "unAuthorizedRequest": false,
  "__abp": true
}

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions. —> System.Data.Entity.Core.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

I wonder to know how to return -1 instead of the error message.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ryancyqcommented, Dec 15, 2017

@KennyEng12 It is because when catch( DbUpdateConcurrencyException ex) { return -1; } did catch the exception, the changes to the entities still remain intact. Therefore, when exiting the method, Abp.WebApi.Uow.AbpApiUowFilter will try to complete the UOW and save any changes made to the entities into the db

If you want to handle the DbUpdateConcurrencyException in your method, i would suggest that you begin a new UOW scope (using IUnitOfWorkManager) to encapsulate all your code that changes the entities, then place the try/catch around the new UOW scope.

0reactions
KennyEng12commented, Dec 18, 2017

@ryancyq Thanks for the suggestion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Return in catch block?
Return usually means "all execution in the method stops and control passes back to the caller" while the finally block always runs when...
Read more >
Can we have a return statement in the catch or finally ...
Yes, we can write a return statement of the method in catch and finally block. There is a situation where a method will...
Read more >
Return Statement in Try-Catch - Java Exception Handling ...
If both catch and finally return, the receiving method will get the returned value from the finally block. Returning referential values ...
Read more >
Can we use a return statement in catch block? Is it a good ...
You can return in the finally block, there's nothing stopping you from doing this. It's probably not a great idea though, since it...
Read more >
Return Statement in Try Catch Finally Block in Java
In the preceding code, finally block overrides the value returned by try block. Therefore, this would return value 50 because the value returned...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found