Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 01-28-2008, 04:43 PM
Member
 
Join Date: Jan 2008
Posts: 14
Joe2003 is on a distinguished road
try catch!?
Hey

I have a load of check functions, TONS of them. Anyway each one returns a boolean value, all need to return true. Is there a way of implementing some form of try catch that will "catch" an error of false being returned? I dont want to have to include loads of ifs, is there a way round this, ie generating my own exception, which is "returned = false"? Thanks

Code:
try { checkName(); checkAge(); checkFace(); checkFoot(); checkGas(); checkMate(); } catch() { System.out.println("A false was returned"); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 04:51 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
Why not have the check functions return void and then throw an exception if a validation failure occurs, the function could wrap up the details inside the exception
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 08:51 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
For example
To add to jelly's reply, here's an example:
Code:
import java.util.*; public class Main{ public static void main(String[] arg){ int number = 10; try{ number = addInteger(number, -5); System.out.println("number = " + number); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } pause(); } public static int addInteger(int number, int positiveNumber) throws Exception { if (positiveNumber < 0){ // Oops! throw new Exception("positiveNumber cannot be negative"); } else { return number + positiveNumber; } } protected static void pause(){ Scanner scanner = new Scanner(System.in); scanner.nextLine(); } }
will output
Code:
Error: positiveNumber cannot be negative
Hope that helped you.
__________________
If your ship has not come in yet then build a lighthouse.
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 issue curtis_fraser Advanced Java 2 12-14-2007 12:04 AM
Try Catch Renegade85 New To Java 4 12-03-2007 05:10 PM
when to use try...catch javaplus New To Java 2 11-18-2007 09:52 PM
try...catch block javaplus New To Java 3 11-06-2007 08:53 PM
Use try and catch zoe New To Java 2 07-25-2007 08:50 PM


All times are GMT +3. The time now is 12:36 AM.


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