Home > Back-end >  Java - Avoid Throws Instruction On Controller
Java - Avoid Throws Instruction On Controller

Time:12-20

So, hello, how are you?

I have a controller that calls a Service's method that calls a Utils' method, like this:

Controller:

@RestController
@RequestMapping("/some-pattern")
public class SomeController {

  @Autowired
  SomeService service;

  @PostMapping("/some-url")
  public ResponseEntity<?> someMehhod(@RequestBody Object docs) {

    service.someMethod(docs) ;
    return new ResponseEntity<>(null, HttpStatus.OK);
  }
}

Service:

@Service
public class SomeService {

  @Autowired
  SomeUtils utils;

  public void someMethod(Object docs) throws SQLException{

    //  THIS METHOD CAN RAISE AN SQL EXCEPTIO
    utils.someMehhod(docs);
  }
}

Util:

@Component
public class SomeUtils {

  public void someMehhod(docs) throws SQLException {
    // Some DB operation that need to try/catch or throw SQLException
  }
}

I dont mind to just throw SQLException, i have a handler class that will handle it, my problem its that i gotta insert "throws SQLException" in controller method too but that doesnt seems right to me, it is ok to have this instruction on controller or i have a way to avoid it? I was thinking do 'wrap' SQLException into a custom exception extending RuntimeException. Thougths?

CodePudding user response:

I have to call a procedure on database, só i'm using EntityManagerFactoryInfo to get Connection object and then calling the procedure, like this:

EntityManagerFactoryInfo entityManagerInfo = 
EntityManagerFactoryInfo.class.cast(em.getEntityManagerFactory());
Connection con = entityManagerInfo.getDataSource().getConnection();
CallableStatement cstmt = con.prepareCall("SELECT Schema.Procedure(?,?,?,?) from DUAL");

So there's no problem i create a exception from Runtime and wrap SqlException?

CodePudding user response:

I think its ok to throws exception in controller also, but have 2 suggestions:

  1. use try catch instead of throws
  2. try @SneakyThrows if you are using project Lombok https://projectlombok.org/features/SneakyThrows

CodePudding user response:

The throws SQLException in method signature is the Java way of making developer aware that this need to be handled or at least acknowledged (as part of method signature).This type of exception is called Checked Exception

"feeling controller throwing an exception is not right" - You are totally on right track. You have couple of options here, either you can catch this exception and convert to appropriate HTTP code in Controller itself, or resort to a global ControllerAdvice which can handle these exceptions. I wouldn't try converting it to a runtime exception just to avoid the need to acknowledge that code branching in method signature.

The usefulness of checked exception is a controversial topic. You will find proponents of both side of this debate.

If this checked exception made you think how to handle it, then checkedException serves it's purpose, IMHO (I came back from golang ecosystem where the language make you handle every darn error paths, I see the point of checked exceptions)

  • Related