Re: how to change method return type from void to boolean
- From: mike <mike@xxxxxxxxxxx>
- Date: Wed, 22 Oct 2008 10:04:47 +0200
Lew wrote:
On Oct 21, 3:00 pm, mike <mikaelpetter...@xxxxxxxxxxx> wrote:My idea is to change the update method to return a boolean. True if
myOperation ( in method) is ok and false otherwise.
I add a return statement after catch that returns true. Where shall I
put my return false? Since if something goes wrong an exception is
thrown using the error code ClearCase.ERROR_UNSPECIFIED in
ClearCase.error(). Will the method return in case of exception thrown
( my guess is no)?
Depends on what you mean by "return". It will return, but it will not
execute a 'return' if it executes a 'throw'.
Throwing an 'Error' after catching an 'Exception' is a bit weird.
According to the JLS, ss. 11.5,
<http://java.sun.com/docs/books/jls/third_edition/html/
exceptions.html#11.5>
The class Exception is the superclass of all the exceptions that
ordinary programs may wish to recover from.
and
The class Error and its subclasses are exceptions from which
ordinary programs are not ordinarily expected to recover.
Even a rethrow of the exception only pushes the responsibility for
handling it one layer up. Why not fully handle it at the point where
you caught it?
public void update(final String[] elements,
final String comment, final int flags,
final OperationListener operationListener) {
//Some other code for checking flags here.
try {
myOperation("update", options, elements, comment,
Please use more moderate indentation for Usenet posts. TABs are right
out. Four spaces is about the maximum indent level for Usenet.
Sorry for that I check with my Eclipse and see if I can use spaces instead.
operationListener);
} catch (CcException cce) {
switch (cce.getErrorCode()) {
default:
ClearCase.error(ClearCase.ERROR_UNSPECIFIED);
break;
}
}
}
The method in ClearCase.error() contains:
ClearCaseError error = new ClearCaseError(code, message, throwable);
throw error;
Implicitly thrown Throwables are potentially very confusing - better
to put the 'throw' in the catch block.
public void update(final String[] elements,
final String comment, final int flags,
final OperationListener operationListener) {
final List optionsList = new ArrayList();
// We don't want any prompt
optionsList.add("-f");
if (isSet(flags, ClearCase.RECURSIVE)) {
optionsList.add("-r");
}
if (isSet(flags, ClearCase.FORCE)) {
if (isSet(flags, ClearCase.KEEP)) {
optionsList.add("-ren");
} else {
optionsList.add("-ove");
}
} else {
optionsList.add("-nov");
}
if (isSet(flags, ClearCase.PTIME)) {
optionsList.add("-pti");
}
final String[] options = (String[]) optionsList
.toArray(new String[optionsList.size()]);
try {
ccOperation("update", options, elements, comment, operationListener);
} catch (ClearCaseException cce) {
switch (cce.getErrorCode()) {
case ClearCase.ERROR_NOT_ACCESSIBLE:
ClearCase.error(ClearCase.ERROR_NOT_ACCESSIBLE);
break;
default:
ClearCase.error(ClearCase.ERROR_UNSPECIFIED);
break;
}
}
return true;
}
Put your "return false;" in the block of code that represents a
'false' result. In your example you do not show where even you would
put the 'return true;'.
When no exception is thrown from the ccOperation() then refresh is ok.
When we get an exception is thrown the update fails and is false. However my problem is that I need to determine what was wrong and show it to the user ( in gui layer).
This is the calling method ( from gui layer that calls above method):
public IStatus visit(IResource resource, IProgressMonitor monitor) {
//Remove irrelevant code
try{
ClearcasePlugin.getEngine().update(new String[] { resource.getLocation().toOSString() }, getComment(),ClearCase.PTIME, null);
}catch (ClearCaseException cce) {
switch (cce.getErrorCode()) {
case ClearCase.ERROR_NOT_ACCESSIBLE:
//Eclipse gui ex
result = new Status(IStatus.ERROR,ID,TeamException.NOT_ACCESSIBLE,MessageFormat.format(
Messages .getString("ClearcasePlugin.error.update.notAccessible"), new Object[] { cce.getElements() }),null);
break;
default:
//More code ...
}
}
//More code
}
My guess is that the catch block should return 'false' instead of
throwing an 'Error'.
How do I determine what happened? What was the error? Maybe it is not of interest to the end-user to know why it went wrong? So if an exception is thrown in update() then we just catch it and return false?
I appreciated your input for a discussion a lot.
By the way, how did the code look like?
br,
//mike
--
Lew
.
- Follow-Ups:
- Re: how to change method return type from void to boolean
- From: Hendrik Maryns
- Re: how to change method return type from void to boolean
- References:
- Prev by Date: Re: does anyone have a repeat permutation program?
- Next by Date: Java and mysql
- Previous by thread: Re: how to change method return type from void to boolean
- Next by thread: Re: how to change method return type from void to boolean
- Index(es):
Relevant Pages
|