Results 1 to 3 of 3
  1. #1
    AWJustAGuy is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default How can I use try/catch?

    I want to do something like
    input something from text field into an integer
    if that thing is not an integer, send an error message

    So I was trying to use this
    try{
    int x = Integer.parseInt(**string from textfield here**);
    }
    catch (????
    {
    //do something error related if it doesn't work.
    }

    However I couldn't find a way to make it work. I tried catch(TypeMismatchException) but it said no, you can't do that, you silly boy.
    What's a way to do this, or is there some other handy dandy function I'm not using?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: How can I use try/catch?

    You can always look at the Javadoc on how to use the class. You will find the description of what the class do. The description of each method including the parameters required by the method, the return type and the exception that might be thrown by those method.

    For the Integer.parseInt(String s) method it throws the java.lang.NumberFormatException exception. For details you can find it here Integer (Java Platform SE 7 )

  3. #3
    AWJustAGuy is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How can I use try/catch?

    Ah, excellent! I couldn't find that when I was searching, that's what I was looking for! Thanks :)

Similar Threads

  1. Try and Catch's
    By dougie1809 in forum New To Java
    Replies: 10
    Last Post: 03-15-2012, 03:23 AM
  2. Try... Catch?
    By Callofdudey in forum New To Java
    Replies: 3
    Last Post: 10-06-2011, 09:29 PM
  3. try and catch
    By Bimz in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 09:44 AM
  4. try catch help
    By vividcooper in forum New To Java
    Replies: 8
    Last Post: 02-11-2010, 09:00 AM
  5. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •