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 2 of 2

Thread: try catch needed? Or recommended?

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default try catch needed? Or recommended?

    I am just recently learning about exception handling and I have yet to come across an example where Try catch was the only way to handle the error. Everything else we have come across we could use some sort of manual while loop condition to keep the user from screwing up. I am just wondering where try catch is needed and while loop will not suffice, as well as what are the advantages of try catch over using your own special way of handling the user input. Is it just to keep the code readable and pleasant to the eye?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: try catch needed? Or recommended?

    The general rule is that, if you can avoid a try-catch using checks (ifs etc.) before an exception would be thrown, do it. Exceptions do have an additional cost to them when compared with pre-checks. However, sometimes Exceptions are unavoidable. When defining APis, exceptions help enforce the rules of that API. As no API can predict every situation their library will be put through, exceptions are added into method signatures to tell clients, if they break the rules, I will not accept it and I will throw an error/exception.

    Ask yourself, should the performance for every client take a hit by doing all these checks on the off chance that a client makes a mistake? or should they leave that stuff the the developer? the latter makes more sense.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Try and catch
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2013, 05:58 AM
  2. Grails Developer Needed - URGENTLY NEEDED *WORK FROM HOME*
    By IngeniumR in forum Paid Java Projects
    Replies: 0
    Last Post: February 5th, 2013, 07:19 AM
  3. try/catch..help needed soon as possible
    By safra in forum Exceptions
    Replies: 1
    Last Post: September 19th, 2010, 08:46 AM
  4. Recommended book for java newblet?
    By Morevan in forum Java Theory & Questions
    Replies: 2
    Last Post: January 5th, 2010, 05:18 AM