3

We all know that SQLException is a checked Exception and most of us agree that checked Exception are verbose and leads 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)

MRalwasser
  • 15,605
  • 15
  • 101
  • 147

2 Answers2

5

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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
3

If you want to treat a checked exception as an unchecked one, you can do

Up to Java 7 you can do

} catch(SQLException e) {
   Thread.currentThread().stop(e);
}

However in Java 8 you can do

/**
 * Cast a CheckedException as an unchecked one.
 *
 * @param throwable to cast
 * @param <T>       the type of the Throwable
 * @return this method will never return a Throwable instance, it will just throw it.
 * @throws T the throwable as an unchecked throwable
 */
@SuppressWarnings("unchecked")
public static <T extends Throwable> RuntimeException rethrow(Throwable throwable) throws T {
    throw (T) throwable; // rely on vacuous cast
}

and call

} catch(SQLException e) {
   throw rethrow(e);
}

Checked exceptions are a compiler feature and are not treated differently at runtime.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    You shouldn't do this. `thread.stop` is inherently unsafe and thus deprecated. (http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#stop%28java.lang.Throwable%29) – dogbane Dec 07 '10 at 11:04
  • 1
    It inherently unsafe to stop a random thread which doesn't expect to be stopped. However in "stopping" the current thread it no more unsafe than throwing an unchecked exception because there is no real difference. – Peter Lawrey Dec 07 '10 at 11:30
  • @reto perhaps you can say why. Is your objection technical or it just looks like a bad idea? – Peter Lawrey Sep 28 '12 at 10:18
  • 1. You use a deprecated method to stop your thread. 2. You stop your thread! 3. the normal logging facilities won't be able to log the exception (try/catch block at the root of the execution tree). – reto Sep 28 '12 at 10:39
  • 1
    1. It is deprecated because it can have unpredictable behaviour if you use it on anther thread, if you use it on the same thread it is predictable. 2. It doesn't really stop the thread, it triggers an exception which is the same as throwing it. 3. You can catch and log the exception in the normal way nothing has changed in that regard. In fact it is easier to catch and log the correct exception if you do it this way than if you wrap it. – Peter Lawrey Sep 28 '12 at 11:06
  • Unchecked exceptions aren't supposed to obligatorily kill a thread but to be cough on the higher level (for cleaner code purpose). Your approach just throws `UnsupportedOperationException` and hides the real case. – schaffe Dec 19 '16 at 16:25
  • @schaffe it does in Java 8 which wasn't released in 2010. – Peter Lawrey Dec 20 '16 at 10:06
  • 1
    @PeterLawrey Wow, this is the most tricky and elegant hack I have ever seen in Java! We have spent about half an hour in our office discussing your solution however still have a few questions. – schaffe Dec 20 '16 at 15:18
  • 1
    @PeterLawrey I have posted another question related to your updated answer http://stackoverflow.com/q/41246793/2856227 – schaffe Dec 20 '16 at 16:25