Results 1 to 5 of 5
Thread: Try Catch
- 12-03-2007, 03:13 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 25
- Rep Power
- 0
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");
}Last edited by Renegade85; 12-03-2007 at 03:27 PM.
- 12-03-2007, 04:18 PM #2
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.
- 12-03-2007, 04:45 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 25
- Rep Power
- 0
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" );
}
- 12-03-2007, 04:59 PM #4
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.
I didn't know that you had specific exceptions that were being thrown. Sorry.
- 12-03-2007, 05:10 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
try catch!?
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 01-28-2008, 08:51 PM -
Try Catch issue
By curtis_fraser in forum Advanced JavaReplies: 2Last Post: 12-14-2007, 12:04 AM -
when to use try...catch
By javaplus in forum New To JavaReplies: 2Last Post: 11-18-2007, 09:52 PM -
try...catch block
By javaplus in forum New To JavaReplies: 3Last Post: 11-06-2007, 08:53 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 08:50 PM
Bookmarks