javaexceptionguavapreconditionsunchecked-exception

Guava Preconditions RuntimeExceptions handling


As I understood, we use Guava Preconditions to fail fast, before changing some objects states (a nice answer here from stackoverflow). And this is good. However it throws Runtime exceptions and this is not the favorite exceptions for the user of an application (500 errors and so on ...). So I need you to give me some help in design.

I have an interface that declares many methods. Each method has arguments which must be controlled (ex: not null). So in the implementation class I use instructions like the following :

Preconditions.checkNotNull(fooObj);

However the program calling this API might crash due to a runtime exception, that is in this case NullPointerException.

So how do you handle these unchecked exceptions?

Thank you.

-------- EDIT The app layers :


Solution

  • A failure of a precondition means that your program has a bug. Users should not encounter these unless they've found a bug in your program.

    Your program should in general show some kind of error message to users in case of an error, but more to the point, you should get informed so you can fix the bug in the first place.