Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Should I use an exception here?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Should I use an exception here?

    Firstly, hi! First time posting here [if this is the wrong area sorry!].

    Basically I'm searching through an array of objects and comparing strings. If there's a match (there can only be a single match) I return the object.

    What do I do if there isn't a match? Should I throw an exception? If so, what type?

    I'm fairly new to Java, and just learning about exceptions, so sorry if it's an obvious question

    Thanks!

    Tom


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,097
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Should I use an exception here?

    This is dependent upon what you are doing, and how you want to implement it. An exception defines an error during the program flow - if you believe not finding a solution to be an error - for example the input array is faulty - than throw an exception. Otherwise, an alternative solution could be to return some defined value (such as null) which must be checked by callers, and if the return value is identical to the defined value nothing was found.
    What Is an Exception? (The Java™ Tutorials > Essential Classes > Exceptions)

  3. The Following User Says Thank You to copeg For This Useful Post:

    whity (May 4th, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Should I use an exception here?

    Welcome to the forums whity.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Should I use an exception here?

    Thanks, copeg. I think I get it!
    It isn't an error to not find a result, but I hadn't thought to just return null.

    And thanks for the welcome, JavaPF

Similar Threads

  1. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  2. can someone please help me im getting exception
    By kristynrod in forum Exceptions
    Replies: 2
    Last Post: March 15th, 2011, 04:40 PM
  3. DAO exception
    By nrao in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 13th, 2010, 11:22 AM
  4. Exception during xml validation
    By vijeta in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 6th, 2010, 01:50 AM
  5. Exception Errors
    By TIMBERings in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 01:13 AM