Skip to content

How to return -1 in catch block. #2732

@KennyEng12

Description

@KennyEng12

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions