1 - 4 : 値チェック(ASSERT)

public class AssertTest {

public static void main(String args[]) {

int a = 10;

int b = 11;

// a == bであれば正常

// a != bであればエラー(invalidと表示)

Utility.assert( a == b, ""aとbの値が異なります。"");

}

}

[Utility.java]

public class Utility {

public static void assert(boolean cond, String message) {

if (cond == false) {

throw (new InternalError(message));

}

}

public static void assert(boolean cond, Object obj) {

if (cond == false) {

throw (new InternalError(obj.toString()));

}

}

public static void assert(boolean cond, Object obj, String message) {

if (cond == false) {

throw (new InternalError(obj.toString() + "" : "" + message));

}

}

public static void assertCond(boolean preCond,

boolean cond, String message) {

if (preCond) {

if (cond == false) {

throw (new InternalError(message));

}

}

}

public static void assertCond(boolean preCond,

boolean cond, Object obj) {

if (preCond) {

if (cond == false) {

throw (new InternalError(obj.toString()));

}

}

}

public static void assertCond(boolean preCond,

boolean cond, Object obj, String message) {

if (preCond) {

if (cond == false) {

throw (new InternalError(obj.toString() + "" : "" + message));

}

}

}

}

実行結果

java.lang.InternalError: aとbの値が異なります。

at Utility.assert(Utility.java:4)

at AssertTest.main(AssertTest.java:8)

Exception in thread ""main""