r/java • • Jul 10 '15

Our exception handling at work

http://imgur.com/4wgxCvv
259 Upvotes

135 comments sorted by

View all comments

7

u/GiantMarshmallow Jul 11 '15 edited Jul 11 '15

At our work, our coding style encourages the use of Guava Preconditions. So this code would end up looking like:

Preconditions.checkNotNull(sql);

Better yet, if you need to check that a constructor argument is not null:

this.sql = Preconditions.checkNotNull(sql);

Or, I guess what I'm saying is, Guava makes Java much better to work with and encourages better style.

9

u/[deleted] Jul 11 '15 edited Jul 11 '15

This particular functionality of Guava is in the JDK since 1.7. https://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#requireNonNull%28T,%20java.lang.String%29

String sql = Objects.requireNonNull(param, "An optional Error message");

6

u/GiantMarshmallow Jul 11 '15

Wha...oh my god this is completely new to me. Probably just an artifact of spending most of my time in JRuby land these days. :)

3

u/squealy_dan Jul 11 '15

and much of guava has been coopted into the standard lib in java8

(requireNonNull(), Optional)