0

What is the Java equivalent this?

assert(0 && "description");

I've tried

assert(false && "description");

But Java types are too strict for that. Any tips on the idiomatic way to add a description to an assert statement?

khelwood
  • 55,782
  • 14
  • 81
  • 108
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • 2
    What is that supposed to do? `"description"` is not a boolean value. What case are you trying to "assert"? –  Jul 23 '20 at 10:20
  • in c++ one would often write a description of what was violated in the assert. For instance, `assert(0 && "one should not get here ...");` – OrenIshShalom Jul 23 '20 at 10:21
  • in c++ when the assertion is hit, the description will be printed to screen, helping in debug by describing what bad thing just happened – OrenIshShalom Jul 23 '20 at 10:22
  • Hopefully you don't think Java's assert mechanism is used for the same things as in C++. It's definitely not for regular debugging which seems to be your goal. – Kayaman Jul 23 '20 at 10:39
  • Are you maybe looking for e.g. `Objects.requireNonNull()` or something similar where you can provide a message? –  Jul 23 '20 at 10:47

1 Answers1

2

You can add a description to an assertion using:

assert false : "description";

This is described as

assert Expression1 : Expression2 ;

on https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html

khelwood
  • 55,782
  • 14
  • 81
  • 108