Difference between using assert and throwing AssetionError exception in java

exception, java

Solution

The main difference is that `assert` is not guaranteed to be processed, unless assertions are explicitly enabled (either via the -ea option to `java`, or programmatically). On the other hand, throwing a `new AssertionError()` will always work.

Some reading information: Programming with Assertions

Problem

To do internal logic checks, there are two ways in Java, - use the assert keyword: e.g, assert(x>y); - manually throw assertion error: e.g, if(y>x) throw new AssertionError(); What are the differences among above two methods( performance wise, programming flexibility, etc.? Which one is considered as a good programming practice?

Original source