Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2007, 04:13 PM
Member
 
Join Date: Dec 2007
Posts: 25
Renegade85 is on a distinguished road
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.

Quote:
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 04:27 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-03-2007, 05:18 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-03-2007, 05:45 PM
Member
 
Join Date: Dec 2007
Posts: 25
Renegade85 is on a distinguished road
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

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-03-2007, 05:59 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-03-2007, 06:10 PM
Member
 
Join Date: Dec 2007
Posts: 25
Renegade85 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
try catch!? Joe2003 Advanced Java 2 01-28-2008 09:51 PM
Try Catch issue curtis_fraser Advanced Java 2 12-14-2007 01:04 AM
when to use try...catch javaplus New To Java 2 11-18-2007 10:52 PM
try...catch block javaplus New To Java 3 11-06-2007 09:53 PM
Use try and catch zoe New To Java 2 07-25-2007 09:50 PM


All times are GMT +3. The time now is 03:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org