当前位置: 代码迷 >> J2SE >> Exception in thread "main" java.lang.AssertionError:
  详细解决方案

Exception in thread "main" java.lang.AssertionError:

热度:72   发布时间:2016-04-24 13:04:58.0
我认为这道scjp题的答案错了,大家看看我说的对不?
Question 65
Given:
12. public class AssertStuff {
13.
14. public static void main(String [] args) {
15. int x= 5;
16. int y= 7;
17.
18. assert (x> y): “stuff”;
19. System.out.println(”passed”);
20. }
21. }And these command line invocations:
java AssertStuff
java -ea AssertStuff
What is the result?
A. passed
stuff
B. stuff
passed
C. passed
An AssertionError is thrown with the word “stuff” added to the stack
trace.
D. passed
An AssertionError is thrown without the word “stuff” added to the
stack trace.
E. passed
An AssertionException is thrown with the word “stuff” added to the
stack trace.
F. passed
An AssertionException is thrown without the word “stuff” added to the
stack trace.


Answer: C

我的答案是,出现:
Exception in thread "main" java.lang.AssertionError: just do it!!
at wren4255.main(wren4255.java:51)

------解决方案--------------------
你jdk的版本是 1.4以上的吗?
------解决方案--------------------
assert 断言还没学。 Mark 学习。
------解决方案--------------------
正确答案是C!
------解决方案--------------------
这是assert的一种用法:
assert exp1:exp2
当程序运动到assert(x>y):"stuff"的时候,exp1为false,后面的表达式的结果会被计算出来并作为AssertionError得构造器参数,而当exp1为true,会忽略exp2;
但这些都不影响最后System.out.println("passed")的输出!

------解决方案--------------------
另外:
1,当java AssertStuff的时候,assert那条语句是忽略的,所以输出passed;

2,而当java -ea AssertStuff,assert语句是起作用的,由于exp1为false,所以就输出An AssertionError is thrown with the word “stuff” added to the stack 
trace;如果你把x>y改成x<y,则会输出passed!
 
  相关解决方案