2011-08-13 99 views

回答

2
try { 
    assert false; 
    System.out.println("Assertions disabled."); 
} 
catch(AssertionError ae) { 
    System.out.println("Assertions enabled."); 
} 
2

這個怎麼樣?我不知道Java的,但我認爲這可能工作:

try { 
    assert false; 
    System.out.println("assertions are disabled"); 
} catch (AssertionError e) { 
    System.out.println("assertions are enabled"); 
} 
0

性能更好的解決方案(不拋出異常)是:

boolean assertionsEnabled = false; 
assert (assertionsEnabled = true);