TestAssert2.java
Select all
/* TestAssert2.java CIS 260 2/16/2005 David Klick Demonstrates the use of the Java assertion feature passing an argument to the AssertionError constructor. */ public class TestAssert2 { public static void main(String[] args) { double a = 3.5, b = 5.2; System.out.println(divide(a, b)); System.out.println(divide(a, 0.0)); } static double divide(double x, double y) { // note that division by 0 in floating point // numbers is actually allowed in Java, but // now we are disallowing it assert y != 0.0 : "Attempted division by 0"; return x / y; } } /* Sample output: 0.673076923076923 Exception in thread "main" java.lang.AssertionError: Attempted division by 0 at TestAssert2.divide(TestAssert2.java:12) at TestAssert2.main(TestAssert2.java:5) */