how to change method return type from void to boolean

M

mike

Hi,

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)?

br,

//mike



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,
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;
 
L

Lew

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>

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.
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.

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;'.

My guess is that the catch block should return 'false' instead of
throwing an 'Error'.
 
M

mike

Lew said:
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>

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.
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
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mike schreef:
Sorry for that I check with my Eclipse and see if I can use spaces instead.

I have made a project ‘Test Java’ in Eclipse and set project specific
properties for the Java Code Style, where you can set it to use spaces
only. Then if I want to post some code here, I first copy it in there,
hit Ctrl-Shift-F and copy it here.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkj+4KoACgkQBGFP0CTku6Mz1gCfYTTVS81RO85fMsosHAlQsoC+
q6sAoKTQW4m3h1nca4zhDh8W65Um4cSc
=47XR
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top