Home > other >  Can adding a try/catch mess with the code logic
Can adding a try/catch mess with the code logic

Time:10-06

I have this code which retrieves by population by city:

@GetMapping("/city")
  public ResponseEntity<List<CityDto>> getPopulationPerCity {
    List<CityDto> city= cityService.getPopulationPerCity(city);
    return return ResponseEntity.ok(city);
}

And what I did was I sourrounded it with a try/catch like this in order to log if the request was successful or not:

@GetMapping("/city")
public ResponseEntity<List<CityDto>> getPopulationPerCity {

List<CityDto> city = null;
try {
city= cityService.getPopulationPerCity(city);
log.info("The city population request is successful")
}  catch (Exception e) {
log.info("The city population request is failed")
}
return ResponseEntity.ok(city);

Would this affect in any way the logic behind?

CodePudding user response:

First of all you need to fix the compilation issues as below, you have declared city twice in method:

GetMapping("/city")
public ResponseEntity<List<CityDto>> getPopulationPerCity {

List<CityDto> city = null;
try {
city= cityService.getPopulationPerCity(city);
log.info("The city population request is successful")
}  catch (Exception e) {
log.info("The city population request is failed")
}
return ResponseEntity.ok(city);
}

It won't Impact this method logic, but if there is some other method which is doing something special, if exception is occurred, then that will get impacted. And they would need to handle this null rather.

So if we see from this method only perspective then answer would be NO, but if we see the bigger picture, then answer would be YES, it might impact the system logic.

CodePudding user response:

You Said:

And what I did was I sourrounded it with a try/catch like this in order to log if the request was successful or not:

Would this affect in any way the logic behind?

So the answer is NO.

CodePudding user response:

First off, I would suggest removing the ResponseEntity<List<CityDto>> from the return type. You quite probably don't need it. Spring will handle it.

To answer your question, in the specific code you provided:

@GetMapping("/city")
public ResponseEntity<List<CityDto>> getPopulationPerCity {

List<CityDto> city = null;
try {
List<CityDto> city= cityService.getPopulationPerCity(city);
log.info("The city population request is successful")
}  catch (Exception e) {
log.info("The city population request is failed")
}
return ResponseEntity.ok(city);

It won't work. Because you of the scope of the city variable. If you check your try, you can see that you are creating another list just named the same, but it will be inside the try block, not outside. So totally independent of your city service, it will return null. Or better said throw an error in your IDE because of re-declaring it.

What you can do is add the return statement inside the try block and in at the end of the method just return an empty list. This is much better than null.

But the general answer does try-catch impact logic, no.

  • Related