will throw a RuntimeException when assertions are not enabled - Java java.lang

Java examples for java.lang:Exception

Description

will throw a RuntimeException when assertions are not enabled

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        checkAssertionsEnabled();//w  ww.  j a v  a  2s.  co m
    }

    /**
     * will throw a RuntimeException when assertions are not enabled
     */
    public static void checkAssertionsEnabled() {
        if (!isAssertionsEnabled())
            throw new RuntimeException(
                    "Assertions not enabled by the VM. Add \"-ea\" as argument.");
    }

    /**
     * @return whether assertions are enabled by the JVM
     */
    public static boolean isAssertionsEnabled() {
        boolean assertionsActive = false;
        try {
            assert (false);
        } catch (AssertionError e) {
            assertionsActive = true;
        }
        return assertionsActive;
    }
}

Related Tutorials