Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between java ' assert 'and ' if () {} else exit;'

Tags:

java

assert

what is the difference between java assert and if () {} else exit;?

can i just use if () {} else exit instead of assert ?

like image 549
jcy Avatar asked May 16 '12 12:05

jcy


People also ask

What is the difference between if and assert?

That an “Assert” is used only for validations, where an “If” clause is used for the logic within our code. We can use an “If” clause to determine whether our automation should follow one path or another, but an “Assert” statement to validate the elements within those paths.

What is assert in Java?

An assertion is a statement in Java which ensures the correctness of any assumptions which have been done in the program. When an assertion is executed, it is assumed to be true. If the assertion is false, the JVM will throw an Assertion error. It finds it application primarily in the testing purposes.

What is difference between assert and verify?

Difference between Assert and Verify in selenium These assertions are used as checkpoints for testing or validating business-critical transactions. In case of verify, tests will continue to run until the last test is executed even if assert conditions are not met.

What if assert fails in Java?

An assertion allows testing the correctness of any assumptions that have been made in the program. An assertion is achieved using the assert statement in Java. While executing assertion, it is believed to be true. If it fails, JVM throws an error named AssertionError.


7 Answers

A bit of google maybe ?

" The main thing you should keep in mind is that the if-else statement should be used for program flow control and the assert keyword should only be used for testing purposes. You should never use asserts to actually perform any operation required for your application to work properly. According to Sun's official Java documentation: "Each assertion contains a boolean expression that you believe will be true when the assertion executes." "

Read more: http://wiki.answers.com/Q/What_is_the_difference_between_assert_keyword_and_if_keyword_in_java#ixzz1v2GGfAhq

like image 61
Depado Avatar answered Oct 03 '22 16:10

Depado


you could, assert is specifically designed to assert some part of code,

assert will throw AssertionError if it fails to assert


Also See

  • assert reference
like image 40
jmj Avatar answered Oct 05 '22 16:10

jmj


I can just ignore the assertion

class A{

public static void main(String[] args) {
    assert false;
  System.out.println("hi");
}
}

This code will print hi by default

$ java -cp . A
hi
$ java -ea -cp . A
Exception in thread "main" java.lang.AssertionError
    at A.main(A.java:6)
like image 44
Tiago Peczenyj Avatar answered Oct 05 '22 16:10

Tiago Peczenyj


can I just use if () {} else exit instead of assert ?

Yes you could, but ...

  • you wouldn't be able to turn of the test unless you made the condition depend on an extra flag (which makes it more complicated, etc),

  • you wouldn't get a chance to perform tidy-up in any enclosing finally block,

  • it would be more complicated to log the cause of the problem than if an AssertionError (or any other exception) was thrown, and

  • if your code needed to be reused (as-is) in another application, your calls to exit could be problematic.

like image 27
Stephen C Avatar answered Oct 01 '22 16:10

Stephen C


if-else is for controlling the flow of your program. assert must not be used for this! Asserts are just for having "checkpoints" in your code to check conditions which "should always" match.

Asserts can be turned on or off when running a program so that you could enable the asserts on your development machine/integration environment and disable them in the released version.

like image 31
Kai Avatar answered Oct 03 '22 16:10

Kai


The assert keyword will only trigger an exception when you enable the assertions with -ea on the command line. The if/else will always execute. On top of that the assert keyword allows to write less verbose code. Compare:

assert parameter != null;

to:

if( parameter != null )
{

}
else
{

}
like image 42
Wim Deblauwe Avatar answered Oct 04 '22 16:10

Wim Deblauwe


Asserts are ignored unless the -ea param is passed:

java -ea myjar.jar

That way, you can use them when you are testing your application, but ignore them at other times.

like image 44
Pablo Avatar answered Oct 02 '22 16:10

Pablo