Adam Bien's Weblog

Java 7/8 And The Built-In Assertion

The java.util.Objects utility class comes with a useful null-check assertions: requireNonNull

Instead of checking a variable with if-statements you can use the built-in method:


import static java.util.Objects.requireNonNull;

(...)
        requireNonNull(this.script, "Cannot initialize ScriptableSink without script.");


The sample above was borrowed from enhydrator.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: effectivejavaee.com.


NEW workshop: HTML 5 and JavaScript Essentials, Three days in April 2017: from Java EE 7 Architectures over Microservices to Performance, Troubleshooting and Monitoring
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

The problem is, it throws NullPointerException. That makes it impossible for the clients to differentiate a bug in the called code (usually an NPE) from a bug in their own code (better expressed through an IllegalArgumentException).

Posted by Ollie on November 02, 2015 at 09:25 AM CET #

Have seen an assert in the wild? http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html

Posted by Arturo Tena on November 04, 2015 at 06:35 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License