Results 1 to 7 of 7
Like Tree3Likes
  • 2 Post By kjkrum
  • 1 Post By gimbal2

Thread: when to use exceptions

  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default when to use exceptions

    I have read through about exceptions and it is mentioned that exception handling usually requires more time and resources and it is advisable that do not use exceptions whenever it is possible to deal with simple, expected situations.

    So, does that means that division by zero should not be handle by Arithmetic Exception since it can be done in following way.
    If so, then when should ArithmetricException be used?

    Java Code:
    int number1;
    int number2;
    
    if (number2 == 0)
      System.out.println("Divisor cannot be zero")
    else
      System.out.println(number1/number2);

    Similarly, null pointer can be done in following way. Am I wrong?
    Java Code:
    if(pointera!= null)
      System.out.println(pointera.toString());
    else
     System.out.println("pointera is null");
    Last edited by dojob; 07-21-2013 at 08:55 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: when to use exceptions

    You didn't bother to reply to two useful responses on your earlier thread. Why should we expect any better behavior this time round?

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

  3. #3
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: when to use exceptions

    Quote Originally Posted by DarrylBurke View Post
    You didn't bother to reply to two useful responses on your earlier thread. Why should we expect any better behavior this time round?

    db
    I am sorry for my own rudeless behaviour for not replying on my own thread and i should have realise this earlier. Thanks for informing so that I won't be making the same mistake next time.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: when to use exceptions

    Don't worry about micro-optimizations. If a method can't comply with its contract, it's almost always better to throw an exception than to count on the programmer checking the return value.
    gimbal2 and dojob like this.
    Get in the habit of using standard Java naming conventions!

  5. #5
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: when to use exceptions

    Quote Originally Posted by kjkrum View Post
    Don't worry about micro-optimizations. If a method can't comply with its contract, it's almost always better to throw an exception than to count on the programmer checking the return value.
    does it mean that the micro-optimization is insignificant and it is better to do in in less trivial way and focus in clean and simple code?

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: when to use exceptions

    Quote Originally Posted by dojob View Post
    does it mean that the micro-optimization is insignificant
    Yes!

    and focus in clean and simple code?

    Yes!

    Generally, clean and simple code is actually more trivial, or as it is also known: the code is more "dumb".

    The Developer Insight Series, Part 1: Write Dumb Code -- Advice From Four Leading Java Developers
    dojob likes this.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: when to use exceptions

    More important even than the fact that the Java compiler does a better job optimizing simple code, is the fact that simple code is easier to understand and maintain. Following the Principle of Least Surprise is almost always more important than shaving a few milliseconds off the execution time.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. exceptions
    By ii88xix88ii in forum JCreator
    Replies: 6
    Last Post: 03-12-2013, 04:07 PM
  2. Exceptions
    By Maya in forum New To Java
    Replies: 2
    Last Post: 05-24-2011, 07:30 AM
  3. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 10:06 PM
  4. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 09:38 AM
  5. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 08:12 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
  •