Results 1 to 5 of 5

Thread: Try Catch

  1. #1
    Renegade85 is offline Member
    Join Date
    Nov 2007
    Posts
    25
    Rep Power
    0

    Question Try Catch

    Hi. I'm looking to make a try catch that will catch two possible errors.

    The first one will be if a character is entered instead of a number i want it to display an error message and keep the program running. (Format error)

    The second one is if an object is not part of an array, i want it to display an error message and keep the program running.

    try{
    //Code here
    }
    catch(WHAT GOES HERE)
    {
    System.out.println("Invalid data entered.");
    }
    catch(WHAT GOES HERE)
    {
    System.out.println("That object does not exist");
    }
    Thanks
    Last edited by Renegade85; 12-03-2007 at 02:27 PM.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    It looks like you might have to write your own exception classes and methods to throw the new exceptions. If you just need to check those two conditions and not alter the execution of the program in anyway, I would just use if statements to print out your error messages.

  3. #3
    Renegade85 is offline Member
    Join Date
    Nov 2007
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    It looks like you might have to write your own exception classes and methods to throw the new exceptions. If you just need to check those two conditions and not alter the execution of the program in anyway, I would just use if statements to print out your error messages.
    I got this sorted. I was just looking to catch two exceptions that kept occuring with my program. I ended up with this

    catch(InputMismatchException e)
    {
    System.out.println("Error. You have entered a character in the number field.");
    }
    catch(NullPointerException e)
    {
    System.out.println("Error. One or both of the teams entered are not valid team names.");
    }
    catch(Exception e)
    {
    System.out.println("Unknown Error" );
    }
    Is there another way of doing this that wouldn't quit the program after an error? Can't think of how you would do an if statement for an InputMismatchError or a NullPointerException.

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    You're right. There is no way to use an if statement for those types of errors. But for what you described in your original post
    The first one will be if a character is entered instead of a number i want it to display an error message and keep the program running. (Format error)

    The second one is if an object is not part of an array, i want it to display an error message and keep the program running.
    it seems that you could just use if statements. Check to see if the object is in an array. If it isn't print something out. Or check to see if a variable is a char. If it isn't print something out.

    I didn't know that you had specific exceptions that were being thrown. Sorry.

  5. #5
    Renegade85 is offline Member
    Join Date
    Nov 2007
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    You're right. There is no way to use an if statement for those types of errors. But for what you described in your original post it seems that you could just use if statements. Check to see if the object is in an array. If it isn't print something out. Or check to see if a variable is a char. If it isn't print something out.

    I didn't know that you had specific exceptions that were being thrown. Sorry.
    My bad, i didn't explain my problem too well. Thanks anyway :)

Similar Threads

  1. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  2. Try Catch issue
    By curtis_fraser in forum Advanced Java
    Replies: 2
    Last Post: 12-13-2007, 11:04 PM
  3. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 PM
  4. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 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
  •