上述程序中语句assert i==10断言i的值为10,如果i的值不为10将抛出AssertionError异常。语句assert sum>10&&sum<5*10:"sum is "+sum断言sum<5*10,如果为false,将抛出带有消息"sum is "+sum的AssertionError异常。
假如现在错误地输入了i<100而不是i<10,就会抛出下面的AssertionError异常:
Exception in thread “main” java.lang.AssertError
at AssertionDemo,main(AssertionDemo.java:7)
假如将sum+=i 错误地输入了sum+=1,就会抛出下面的AssertionError异常:
Exception in thread “main” java.lang.AssertError:sum is 10 at AssertionDemo,main(AssertionDemo.java:7)