Search code examples
javaexceptionsqlexception

How should I wrap a SQLException to an unchecked one?


We all know that SQLException is a checked Exception and most of us agree that checked exceptions are verbose and lead to throw/catch pollution.

Which approach should I choose to avoid SQLException throwing?

Which wrapper/technique/library is recommended? (for example DataAccessException for the Spring folks, but I don't want to use Spring)


Solution

  • Just wrap it as new RuntimeException(jdbce). Or defince your own exception that extends runtime exception and use it. I do not think that any framework is required here. Even spring wraps checked exceptions by unchecked every time it needs it.