sdsdsdsd fdfdfwwq
sdsdsdsd fdfdfwwq

Reputation: 57

correcting the return type of the method

I have the below code as shown below of which I want to correct the return type

  public boolean deleteById(Integer id) throws ResourceNotFoundException {

        abcRepository.deleteById(id);
        return true;
    }

what I am looking to correct it as

if (abcRepository.deleteById(id))
       {
        return true;
       } else 
        return false;

Now this repository is calling the Jpa repository method of which return type in decompiler I have check is shown below

void deleteById(ID var1);

Now please advise how smartly I can change the return type

Upvotes: 0

Views: 597

Answers (1)

Andreas
Andreas

Reputation: 159086

Seems you want your method to return true when an object is deleted, and false when object is not found.

The method you are calling is void because is uses exception ResourceNotFoundException to indicate object not found, so you need to catch that exception and return false, instead of making the exception bubble up:

public boolean deleteById(Integer id) { // <-- removed throws
    try {
        abcRepository.deleteById(id);
        return true;
    } catch (ResourceNotFoundException ignored) {
        return false;
    }
}

Upvotes: 4

Related Questions