A throw statement is used in place of an assert statement because the throw statement can not be disabled and therefore the method is certain to generate an error once control passes beyond all of the return statements.
SCJP2. Please Indent your code using UBB Code
Originally posted by Jose Botella:
My two cents.
A) If the intention of the programmer was to check for certain contraints of arguments to public methods (b1, b2, b3 and i) , it is not possible to replace throw with an assert. However it is rare to use an error for that purpose.
can I then say that the throw statement can now be replaced by an assert statement ?
SCJP2. Please Indent your code using UBB Code
SCJP2. Please Indent your code using UBB Code
Rule: do not use assertions to enforce public usage patterns or protocols
The following public class can be in one of two states: open or closed. It is an error
to open a Connection that is already open, or to close one that is already closed.
However, we would not use an assertion to ensure that these mistakes are not made:
public class Connection
{
private boolean isOpen = false;
public void open() {
// ...
isOpen = true;
}
public void close() {
// BAD!!
assert isOpen : "Cannot close a connection that is not open!";
// ...
}
}
The programmer has attempted to enforce the requirement that a Connection can
only be closed if it is already open.
This usage is valid if and only if the Connection class were a private class, or
were otherwise guaranteed to be invisible to the outside, and if we were willing to
ensure and assume that any code that uses this class is written correctly. In this case,
it would be legitimate to enforce this assumption with an assertion.
However, if the Connection class is used publicly, it would not be surprising to
find a bug in which someone tried to close a Connection that wasn�t open in the
first place. In this case, a regular exception would be better:
public class Connection
{
private boolean isOpen = false;
public void open() {
// ...
isOpen = true;
}
public void close() throws ConnectionException {
if (!isOpen) {
throw new ConnectionException(
"Cannot close a connection that is not open!" );
}
// ...
}
}
If you go the other route, and attempt to ensure that this code is only called from
call sites you control, think twice. Any code you write now may be used or reused
later in a different configuration. Anything can happen after the initial revision, so
it�s best to be on the safe side. Using an explicit exception provides the most information
to a frustrated programmer down the line.
SCJP2. Please Indent your code using UBB Code
"I'm not back." - Bill Harding, Twister
Originally posted by Jose Botella:
Note that even if you are thinking about flow-control assumptions, the checks performed are about arguments to public methods. I think this feature takes precedence because the programmer who wrote F cannot be sure how a client of F will use it.
Originally posted by Marlene Miller:
Well, how do I know the programmer believes this? It depends on what the contract of the method is? It depends on what the specification of the return values of this method is?
Originally posted by Jim Yingst:
Without something like these alternatives, I'm not sure it's productive to worry much about preserving the original programmer's intent here; it was misguided anyway.
So what then determines if a statement or comment is replaceable by an assert statement?
SCJP2. Please Indent your code using UBB Code
"I'm not back." - Bill Harding, Twister
If the intention of the programmer was to check for...
So what then determines if a statement or comment is replaceable by an assert statement?
SCJP2. Please Indent your code using UBB Code
"I'm not back." - Bill Harding, Twister
SCJP2. Please Indent your code using UBB Code
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |