2

I am referring to this post: What does the Java assert keyword do, and when should it be used?

My understanding is that: assertion helps check the validity of an variable, if the check fails, it throws an AssertionError. And this check is usually turned off in production.


Question 1: Why in production we do not need to check validity of parameter? I mean, the user can pass in an invalid parameter to purposely break the program, right?

Question 2: Since assertion is usually turn off in production, does it mean I need to write check and throw exception in addition to the assertion?

Community
  • 1
  • 1
modeller
  • 3,770
  • 3
  • 25
  • 49
  • 1
    Did you read the first comment to the answer? *In fact, Oracle tells you not to use `assert` to check public method parameters. That should throw an `Exception` instead of killing the program.* – Andreas Aug 03 '16 at 14:08

3 Answers3

2

The point is that your users probably expect your application to be robust. They do not want your application to die when some internal assertion doesn't hold.

In other words: test and production differ in many ways; one of them is the handling of errors. A test person might want a quick fail; together with as precise error information as possible. A end user wants to continue whatever he is doing; and if at all; he wants some error code that he can report back to you.

So: you still need dedicated error handling in your code; if some method should fail on "bad" input; then it should throw the corresponding exception. And then some other component should catch that exception and decide what to do from there. In other words: probably you don't want to use asserts at all. As you want to test exactly the same product that the end user will be using. Thus having two different ways of error handling in there is not a helpful pattern.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

Assertion is used to validate some state that your code assume that is true.

In other words if the assertion fails your app world had collapse (or maybe just a bug).

This is in opposite to an error that you can handle (i.e. wrong user input).

For example, lets say that you asked the user for it's phone number. You should not assert if the phone number format is wrong, instead you should check it and respond accordingly (error code or display error message).

On the other side, lets say that you are storing the phone number in a database. When your app loads you connect to the db and from there on you are writing and reading from it, assuming the connection is live. If you are writing a method that suppose to store the phone number in the db you should not write code to check the connection of the database, instead you may add assert to check that the connection is live. That way in development mode you will have a nice readable stack trace if you ever (for some reason) run into a situation where the db was not available at the time of the assert.

Role of thumb: you should assert for things that you relay that are correct and should have no good reason not to be

Matan Lachmish
  • 1,235
  • 12
  • 17
-1

Answer 1: an assert is used to " verify the correctness of an invariant in the code " User input is not an invariant.

Answer 2: Yes but you should probably remove the assert as you are probably not checking an invariant.

Bob Brinks
  • 1,372
  • 1
  • 10
  • 19