• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

AssertionError

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it from
http://www.danchisholm.net/oct1/mybook/chapter23/assertions1.html


class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}

Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions disabled it prints ABC followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example an assert statement could not be used in place of the "throw" statement.

Answer is a,b,d

I am unable to understand statement b. If Assertions are disabled then how it is able to throw AssertionError?
Please clear my doubt

Thanks,
Geeta Vemula
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi geetha..

As per my knowledge....

here you are thowing an exception which is not depends upon enable and disable of assetion at runtime.

if you are using assertion syntx like assert a<b:"message"

then you need to enable assertions at runtime because bydefault it is in disable mode...
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well!!! It is really simple, if you would have run this program, you would have got the reason.

When c.m1(3) will be called, then "default" case will run, and that is "throw new AssertionError()". It is just an error, to throw this error you donot require to enable assertion. There is not statement like "assert boolean:expression" that will depend on enabling or disabling of assertion.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can someone please explain why option d is true.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can someone please explain why option d is true.



As no case has break statement, so everytime assert statement will get executed. Assert could be used with default, but in situation where it is not possible or error to control reach default case.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garima
can someone please explain why option d is true.



if you replace throw new AssertionError(); with assert new AssertionError(); then you would be violating the rule of Assertions which says that the first expression should always result in a boolean value.

Though you could have said something like this:

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

d. In this code example an assert statement could not be used in place of the "throw" statement.



See Harvinder what this statement is saying.

And you said:

default:{
assert (true): new AssertionError();
return "";
}



You cannot do it here Harvinder.

switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:{
assert (true): new AssertionError();
return "";
}



see here no case statement has break; means what? default case will be executed every time you call switch() with any value. That is not the use of assertion.
[ December 22, 2008: Message edited by: punit singh ]
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by punit singh:


As no case has break statement, so everytime assert statement will get executed. Assert could be used with default, but in situation where it is not possible or error to control reach default case.


AND

see here no case statement has break; means what? default case will be executed every time you call switch() with any value.



I dont think that is true.
If the return statement is executed once, it will return from the method.It does not matter if break is present or not.

if a return statement is present in try or catch block ,and if finally block exists ,THAT is the only place were return is over looked . i.e finally will exectue and then only it returns.

Ex:


only after finally is executed , it will return.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I overlooked the return statement!

Now I am also not clear why d is true here ?
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Punit:

see here no case statement has break; means what? default case will be executed every time you call switch() with any value. That is not the use of assertion.


I guess James has clarified your point.

Moreover, that example was only to show how assert could have been put there legally. Also the real exam options would want to test on a legal use of JLS statements and not the appropriate use of JLS statements.
So, incase i did not communicate properly, i just wanted to show a correct legal use of assert statement *syntactically*. Nothing more.

But i do stick by my previous explanation as to the use of a boolean expression in an assert statement being mandatory.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harvinder Thakur:


if you replace throw new AssertionError(); with assert new AssertionError(); then you would be violating the rule of Assertions which says that the first expression should always result in a boolean value.



This is the reason for d to be correct
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok ok I got the point...
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
Originally posted by Harvinder Thakur:


if you replace throw new AssertionError(); with assert new AssertionError(); then you would be violating the rule of Assertions which says that the first expression should always result in a boolean value.

--------------------------------------------------------------------------------

The above reason is not the correct explanation for the option d to be correct.

The reason is because the method is returning int value, it must return something instead of returning void. this is what i thought. Tell me whether I am correct or not.

Thanks,
Geeta Vemula
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to add ..
So it must either throw or return something.

Thanks,
Geeta Vemula
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Expression must return boolean.

As assert statement is written like:

assert boolean;
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ preetha Arun
After reading your post,i had these questions,

The reason is because the method is returning int value


Which method and where int is returned???

it must return something instead of returning void.



Where is void returned ???

So it must either throw or return something.



I think there are some other options,(i guess)
[ December 22, 2008: Message edited by: James Tharakan ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James where is Preetha Arun' post ?
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method m1 is returning String. So it must return something.

class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}



Geeta Vemula.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by punit singh:
James where is Preetha Arun' post ?




My mistake
I meant geeta vemula




[ December 23, 2008: Message edited by: James Tharakan ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by geeta vemula:
method m1 is returning String. So it must return something.



The first three cases would return string,
but default ,its throwing a error at runtime.
I am confused with your question ,hope someone else would answer.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false;//error must return String here or
}

//or here
}



If you want the code look like above then you are right.
As m1() method is returning String then default case must also return String, if you write assert statement.

then we have to write like:

String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false; return "D";
}

}



String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false;
}
return "D";
}


[ December 23, 2008: Message edited by: punit singh ]
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in this case no String object is returned, instead an Assertion error is thrown???
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somewhere i have read if the mothod is having non-void return type then it must return something or else it must throw something.
Even i am not sure about the above statement. Please anyone clear this.

Thanks
Geeta Vemula
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya this is true, either return something or throw any exception/error.

String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}



This is completely valid.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Punit.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Geeta Vemula:
The above reason is not the correct explanation for the option d to be correct.

The reason is because the method is returning int value, it must return something instead of returning void. this is what i thought. Tell me whether I am correct or not.



Well you answer is correct in the context of the method returning a String value.
I could have used the same logic to say that it is "assert boolean" syntax which is the only reason.

But i guess the reason of assert accepting a boolean is correct alongwith the reason put forth by you. That is why i put a return statement in the default block of my previous post, though i explicitly did not mention it as i thought the primary reason was "assert boolean" syntax.
Your perspective is totally valid but for me the primary reason is "assert boolean"
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are all missing the point of why d. is correct. The actual reason is this:

throw new AssertionError();
is equivalent to:
assert false;
only when assertions are enabled when you launch the program. If assertions are disabled, the second statement is not even seen by the JVM. However, the first statement remains regardless of whether assertions are enabled or not.

There was some discussion as to whether a method that returns a String must always return a string. But what would be the point of returning a String when an exception or error that is not caught is thrown? When an exception or error is thrown the method doesn't even return in the proper sense of the term. It just throws an exception or an error.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ruben:

throw new AssertionError();
is equivalent to:
assert false;
only when assertions are enabled when you launch the program. If assertions are disabled, the second statement is not even seen by the JVM. However, the first statement remains regardless of whether assertions are enabled or not.



Can you please elaborate what you are trying to say?
Also let me add, the compiler does not know whether assertions will be enabled or disabled at runtime. Hence, as per the question if we say
assert new AssertionError(); you won't even go past the compiler.
And why would the compiler not let you go past because it expects a boolean as the first expression of an assert. Simple.

But, if you do want to introduce an assert then the syntactically correct example( not necessarily appropriate) is:


Originally posted by Ruben:

But what would be the point of returning a String when an exception or error that is not caught is thrown?



A String return would be required to compile the program.
The assert statement introduced in the default clause won't even compile without the return. Also the use of assert statement, was only to show a legal use and not an appropriate use. Please refer to my previous post.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can someone please explain why option d is true.



geeta answered this question correctly. If I replace throw with assert (not as Harvinder is saying), then the code will not compile



Now this code will not compile. Suppose you pass 10 to the method and assertions are disabled on runtime. Then the default case will get executed. Since assertions are disabled, so assert will not get executed. Now the control will get out of switch. Now what?? The method is over and there is no string value to return. What will the poor JVM do now. So the compiler saved JVM from this confusion by generating a compile time error in the default case. The correct code would be



or

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Ankit,
Actually
OPtion d.In this code example an assert statement could not be used in place of the "throw" statement.

What does the word "In place of" mean ? ?

does that mean ,
1-> default: assert new AssertionError();

or
2->default: assert false;


If it is the second one (default: assert false ,

how can we assume that there is false. Why true can't be there??
:roll:
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever it is i.e. false or true, the code will not compile. If you read the question, it says this

in place of the "throw" statement.



It is not talking about replacing the throw word...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James it is talking about the replacing with the whole assert statement.
Then it's up to you, you can put any boolean.

assert true;
assert false;
assert any_boolean_expression;

But my question is what is the relationship of this assert argument to this question?
Assert statement could be any legal statement.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, "in place of" in this case doesn't mean to substitute "throws" by "assert." If someone asked you to put a for loop in place of a while loop you wouldn't just replace "while" for "for", right?

I am very surprised that this:

won't compile. The compiler is smart enough to distinguish between assertion code that might not exist depending on the execution command, and code that will always throw an exception.

Thanks for explaining this, guys.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A statement is indeed different from a keyword.
The correct perspective thrown by Ankit on how to interpret the throws *statement* has changed the answer (for me atleast).

Yes, folks thanks for pitching in and correcting this oversight on my part.
I agree with Geeta and Ankit and all those who say that the reason why option d is correct is the return statement missing in default statement.

[ December 24, 2008: Message edited by: Harvinder Thakur ]
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harvinder Thakur:
I agree with Geeta and Ankit and all those who say that the reason why option d is correct is the return statement missing in default statement.



Good Boy
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And even if you add the return statement, you can't actually substitute the throws statement by the assert statement (plus the return statement.)

If what you understand by "substituting for" means that it will still compile, then the answer is yes. But I don't think you should understand the question that way. In that case, you could substitute the throws statement by a "return ""; " statement, although they would clearly be not equivalent.

What I think they are asking is if you could make the program logically equivalent by replacing the throws with an assert. In this case the question is moot because the assert in its own won't compile, but I think the intent of the question is to see if you can realize that assert statements are only in effect when assertions are enabled, unlike throws statements.

That's what I think they are asking, anyway. As if Java wasn't complicated enough to master, now I have to decipher the semantics of the English language.

[ December 24, 2008: Message edited by: Ruben Soto ]
[ December 24, 2008: Message edited by: Ruben Soto ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic