Results 1 to 5 of 5

Thread: Exception catch

  1. #1
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default Exception catch

    Hello

    What kind of exception am I supposed to catch when I press a jButton, that needs to take an integer from a field and pass it, but that field is empty(contains nothing).
    Also what kind of exception should I catch if the field contains a string, instead of an integer.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    What kind of exception am I supposed to catch when I press a jButton
    There should not be any exception when a button is pressed.
    take an integer from a field
    I assume that the field contains a String.
    There could be an exception when you try to convert the String to an int if the contents is not numeric.
    Look at the API doc for method you are using to convert the String to an int. The doc should list what exceptions can be thrown for that method.

  3. #3
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Ok, I'll do that, but I still didn't get what exception I am supposed to catch, when I try to pass a string from an empty field(the 1st question). Let me put it to examples, cause clearly many people cannot understand what my problem is.

    If the field contains nothing, no strings, no integers, no chars, nothing and I press the button(call a method), my program freezes, since its expecting some kind of characters in that field(String, int,...), but it doesn't find any. The question was: How should I handle that, without the program freezing

    Quote Originally Posted by Norm View Post
    There should not be any exception when a button is pressed.

    I assume that the field contains a String.
    There could be an exception when you try to convert the String to an int if the contents is not numeric.
    Look at the API doc for method you are using to convert the String to an int. The doc should list what exceptions can be thrown for that method.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    what exception I am supposed to catch, when I try to pass a string from an empty field(the 1st question)
    There should be no exception when getting an empty String from a text field. The exception would occur in a method that tries to convert that String.

    Or is your question: How do I throw my own exception when the program detects that the use has entered invalid data?

    my program freezes,
    Please explain. Are there any error messages? Please copy and paste the full text here.
    Does the program go into an infinite loop somewhere?
    Add lots of println statements to show where the execution flow goes and how the values of variables change.

  5. #5
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default

    When the text Field contains blank, Parsing it to a integer will generate a NumberFormatException (I think) and is a Unchecked exception i.e. is occurred due to either by wrong programming by Developer or by wrong input by the user. They may be irrecoverable.

Similar Threads

  1. try catch exception
    By soxfan714 in forum New To Java
    Replies: 3
    Last Post: 05-05-2011, 11:11 AM
  2. try, catch, exception error
    By Asvin in forum New To Java
    Replies: 5
    Last Post: 04-11-2011, 10:12 PM
  3. Catch exception, return to while loop
    By ChicagoAve in forum New To Java
    Replies: 3
    Last Post: 02-20-2011, 06:44 PM
  4. Replies: 7
    Last Post: 12-30-2010, 01:42 PM
  5. Why can't I catch an exception?
    By glhansen in forum New To Java
    Replies: 5
    Last Post: 03-17-2009, 06:48 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
  •