how to change method return type from void to boolean

Discussion in 'Java' started by mike, Oct 21, 2008.

  1. mike

    mike Guest

    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;
     
    mike, Oct 21, 2008
    #1
    1. Advertising

  2. mike

    Lew Guest

    On Oct 21, 3:00 pm, mike <> 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.

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

    --
    Lew
     
    Lew, Oct 21, 2008
    #2
    1. Advertising

  3. mike

    mike Guest

    Lew wrote:
    > On Oct 21, 3:00 pm, mike <> 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
     
    mike, Oct 22, 2008
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    mike schreef:
    > Lew wrote:
    >> 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.


    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-----
     
    Hendrik Maryns, Oct 22, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    602
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    837
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    882
    S.Tobias
    Jul 22, 2005
  4. Abhishek
    Replies:
    12
    Views:
    841
    Eric Sosman
    Jan 30, 2006
  5. Replies:
    1
    Views:
    435
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page