Results 1 to 6 of 6
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Explaining Exceptions

    I recently finished the chapter on exceptions in "thinking in java" and "sams java in 21 days" I understand the idea's of using it, but I am still a little confused and I come here looking for help with understanding it.

    I get that it's to protect your code, but I just don't see when it becomes important to use. Is it generally expected to use exceptions to exit the program and report where and what happened, or is it to keep the program from exiting and continuing as expected? I have a hard time putting into words what confuses me about exceptions. In sams java 21 it was used to restrict users from entering certain numbers, and most of the examples in thinking in java are more about where it goes when an exception is thrown, and not many practical examples, for most of the book I don't enjoy this type of examples, but in this chapter it seems to be good. Has anyone out there had trouble learning exceptions, and how did you finally manage to understand how they work?

    I understand that you are supposed to guard your code statements with a try catch clause, that way if something happens the code will throw an exception and you can write more code to catch it and manage it, but I don't really get why it's useful to use.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    but I don't really get why it's useful to use

    Well if you don't catch and manage some exceptional situation (like a user pulling a usb drive out while you are writing a file to it) what do you expect your application to do? And how do you expect your application to do that unless you tell it?


    I just don't see when it becomes important to use. Is it generally expected to use exceptions to exit the program and report where and what happened, or is it to keep the program from exiting and continuing as expected?

    I am no expert about this but I think

    (1) It's a good idea to limit exceptions to things that really are exceptional.

    There's nothing exceptional about dividing by zero - that's just a plain programming error, likewise accessing an array at an odd index location or trying to dereference a variable before it is assigned a nonnull value. These things will throw exceptions, but I think of the exceptional as being "not my fault": like the drive being pulled out.

    Likewise don't use exceptions for things that you can reasonably check for (and thereby make "your fault" if they have bad values). For instance if you are dividing by some value and it could be zero, check for that rather than relying on catching an exception to implement your logic.

    (2) If you can recover (ie keep going) do so. It is better to inform the user that the file save operation failed rather than exiting the program. That way they can plug the drive in and try again rather than lose their work.

    (3) Random error codes reported to the user prior to the program exiting serve no purpose unless you are intending to support the application and intend the user to send the codes to you. (In which case offer to print them along with any other information that *you* will find useful.) Error codes used without purpose don't look professional - imho - quite the opposite.

    ---------------------------------

    Variations on try-catch-finally have become commonplace in langauges nowadays. But Google will turn up plenty of detractors, so you are not alone in wondering what the value of this approach is.
    Last edited by pbrockway2; 01-22-2011 at 06:35 AM.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I am a begginner too so my explanation might not as good as some expert but for
    me exception is useful because...

    It let you know what kind of error did you get and by that you will have an
    idea where and how to solve the error occured. Example is when trying to connect to a database.
    When connecting to database there are two possible error that you will have, ClassNotFoundException
    and SQLException. If the error is ClassNotFoundException then you already know that the error is in class
    and will already check 1.) If database is install, if yes is it installed correctly. b.) Is the database's
    library included in your library? etc..

    Imagine you need the user to input only date on a JTextField to check it you have to convert JTextfield.getText()
    to date, if the user did not enter date or a valid date an error will occur, and using exception you can inform
    the user that input is incorrect.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Allow me to also add my $.02.

    While in many of the examples that a single book or class might give you have a finite number of possible inputs, the majority of programs are designed to work with a virtually infinite number of unknown user inputs. What happens when the user attempts to open a file that doesn't exist? What happens when the user unplugs something in the middle of using it? What happens when an internet connection is dropped mid-file transfer or mid client/server message? If you don't use exceptions, then your program just crashes. Game over. But with exceptions, you have the opportunity to recover from unexpected situations. A file not found can turn into a message that asks the user to try a different file. A dropped connection can prompt to try again or wait for a while or even reconnect and resume.

    Exceptions are less about 'protecting' your code and more about not making the user want to burn down your house when all their hard work disappears because your app crashed when they hit an odd key combo by accident. Its an important and vital concept to learn and use, so its a good practice to use it where ever possible. At at the very least it gives the opportunity for you to print an error message and fail gracefully.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    And we can say that Exception plays a big part on your application to be user-friendly.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Thanks very much for helping me understand better, I really appreciate being able to get clarifications from more skilled people.

Similar Threads

  1. Help explaining the difference.
    By Romally in forum New To Java
    Replies: 1
    Last Post: 11-14-2010, 03:40 PM
  2. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 02:44 PM
  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. Replies: 4
    Last Post: 01-01-2009, 01:03 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
  •