Results 1 to 5 of 5
  1. #1
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Should I be using if-else? or try-catch?

    I'm making a sort of notebook application. In the class "NoteBook" there is an array of size 5 which can hold at max 5 "Note" type objects. I want to have a "getNote()" function but the user must specify a valid input (a value between 0 and 4 to be precise). Should I be using a if-else to ensure this is the case or should I be using a try-catch?

    Here's the code I have, which wont compile because I have to have a guaranteed return of type "Note":

    /** Returns a note from the notebook */
    public Note getNote(int x)
    {
    if(x >= 0 && x <5)
    {
    return this.noteBook[x];
    }
    else
    {
    System.out.println("Invalid input, please enter a number between 0 and 5");
    }
    }

    Probably a very obvious answer but I just really dont want to develop bad habits. Thank you.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Should I be using if-else? or try-catch?

    It's an if/else, and you can avoid the hardcoding by using noteBook.length instead of '5'.
    And if it's out of range then I would possibly consider throwing an exception.
    This (to me) does not look like the place to have UI code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Should I be using if-else? or try-catch?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Should I be using if-else? or try-catch?

    Are you correcting me for saying "if-else" as appose to "if/else" or are you telling me to use an if/else statement?
    When I have a GUI set up I wont need a user input the user will simply click which note they want to read but for now its just to test the class' functionality.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Should I be using if-else? or try-catch?

    I'm saying use an if/else, because that's what it is.
    You know the range, so don't rely on the ArrayIndexOutOfBounds exception.

    As for the UI (not GUI), doesn't matter what sort of UI itis, it doesn't belong in your logic code.
    Trust me, it'll be easier to replace your command line UI with a GUI if the command line bits are already separated.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Try and Catch's
    By dougie1809 in forum New To Java
    Replies: 10
    Last Post: 03-15-2012, 03:23 AM
  2. try and catch
    By Bimz in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 09:44 AM
  3. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  4. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 05:39 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •