Re: how to change method return type from void to boolean



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

.



Relevant Pages

  • Re: Validating file names
    ... running Novel, from your G: running Linux file system, from your H: running ... I am not saying one does not exist! ... file file and handle any exceptions that were thrown. ... If an exception is thrown then the specified ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Using an ActiveX ocx control on a Webform
    ... > download the ActiveX I can Automate the ActiveX object through the ... > Exception of type InvalidActiveXStateException was thrown. ... > tried using the item in the design mode of a regular .NET Windows ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Exception-Objekt freigeben
    ... When an exception is thrown, the exception object that is thrown is destroyed ... we have added the AcquireExceptionObject and ReleaseExceptionObject functions. ... then the thrown object is not destroyed by the RTL, but assumed to be in control ...
    (de.comp.lang.delphi.misc)
  • Re: Question about Authorization Manager
    ... They do throw an exception if an empty task is passed for a check. ... second message to be thrown, but I'm guessing that it's a similar ... security block code. ... But I understand that you believe what I was describing was referring to an empty task, i.e. one not containing any operations or nested tasks that contain operations, and this is where we are not in sync with one another. ...
    (microsoft.public.dotnet.security)
  • Re: exception handling in complex Python programs
    ... errors possibly thrown by open, or leave it to the caller? ... def do_something: ... have to couple the exception type with the function --- between file ... Python 3 and an IOError is thrown in the other three cases <bashes ...
    (comp.lang.python)