I have compiled this question with no errors/warnings.
What do you mean for "Assertions should not have side effects." ??
About Question enthuware.ocpjp.v7.2.1565 :
Moderator: admin
-
- Posts: 17
- Joined: Wed Nov 26, 2014 1:59 pm
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1565 :
It is explained in the explanation -
HTH,
Paul.
Observe that the method will be invoked only if assertions are enabled. If the method is doing something important, then the application will not function correctly if assertions are disabled. This is not good.3. Should not be used for side effects. For example:This is not a proper use because here the assertion is used for its side effect of calling of the doSomething() method.Code: Select all
public boolean doSomething() { ... } public void someMethod() { assert doSomething(); }
HTH,
Paul.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1565 :
The right option:
The explanation says:
Code: Select all
public void openSocket(int port) {
Socket s = //valid code to open socket here
...
assert s != null;
}
But why "at the end"? What's the idea? If code to open socket fails for some reason and variable "s" is null, then I got some exception (checked or unchecked) in business logic and won't reach assertion.Assertions should be used for:
...
3. Validating post conditions at the end of any method. This means, after executing the business logic, you can use assertions to ensure that the internal state of your variables or results is consistent with what you expect. For example, a method that opens a socket or a file can use an assertion at the end to ensure that the socket or the file is indeed opened.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1565 :
How will you check post-conditions before the method is done with its execution? They are called post-conditions precisely because they will hold true only after the method has done its work.But why "at the end"? What's the idea? If code to open socket fails for some reason and variable "s" is null, then I got some exception (checked or unchecked) in business logic and won't reach assertion.
Code may not always result in an exception. It may execute without an exception but yet somehow produce wrong results, for example, due to badly written multi-threaded code. You could use assertions to check for such things at the end of the method. If the assertion fails, you know there is some problem with the code that you need to investigate. That's the whole point of assertions. They produce outputs that illuminate the parts of code that execute successfully (i.e. without exceptions) yet are producing unexpected results.
HTH,
Paul.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1565 :
Thank you, Paul, now got itadmin wrote:That's the whole point of assertions. They produce outputs that illuminate the parts of code that execute successfully (i.e. without exceptions) yet are producing unexpected results.
Who is online
Users browsing this forum: No registered users and 4 guests