Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java excetion /return problem

Options
  • 25-04-2004 2:03pm
    #1
    Registered Users Posts: 919 ✭✭✭


    just a simple question as to which i should be doing. I hava a list of names and a method that allows you to delete one. The thing is you are passed the name you want to remove and if that name isn't there then what. Two choices are available throw an exception or simply have a return if it was successful or not.

    CODE:

    public void deleteUser(String userName) throws DeleteException{
    if (username.equals(user))
    user = "";
    else
    throw new DeleteExcption();
    }

    public boolean deleteUser(String userName){
    if (username.equals(user))
    user = "";
    return true;
    else
    return false
    }


    Which is better or more in line with standards???


Comments

  • Registered Users Posts: 6,306 ✭✭✭OfflerCrocGod


    Do NOT trow exceptions in a program and then have the program continue run!, exceptions are expensive operations true false 1 or -1 are acceptable, no exceptions.


  • Registered Users Posts: 919 ✭✭✭timeout


    Do NOT trow exceptions in a program and then have the program continue run!,
    I thought the idea of throwing an exception was to unsure if something went wrong that it was caught and your program would recover from it?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Originally posted by timeout
    I thought the idea of throwing an exception was to unsure if something went wrong that it was caught and your program would recover from it?
    Normally Exceptions would be used in situations where the error is quite serious to the running of the program, and/or where a number of possible things can go wrong, where you need as much information about the error as possible (File writing is a perfect example - the amount of things that can go wrong when writing to disk). As Offler says, simply throwing one when you hit an error, without any real need is an expensive operation.

    In your above program for example, what else can go wrong except that the name doesn't exist? Nothing as far as I can see. So in that case, the boolean return is the sensible choice - the name is either found and deleted, or it doesn't exist - A or B, true or false...

    Hope that clears up stuff for you

    [Edit - Exceptions can also be used as reminders to programmers that something serious can happen - i.e. the program won't compile if you don't catch the error.


  • Registered Users Posts: 919 ✭✭✭timeout


    Thanks for that. Sorts out a few problems for me and shortens my code a bit now.

    Timeout


Advertisement