Results 1 to 3 of 3
Thread: try catch!?
- 01-28-2008, 03:43 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 21
- Rep Power
- 0
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
Java Code:try { checkName(); checkAge(); checkFace(); checkFoot(); checkGas(); checkMate(); } catch() { System.out.println("A false was returned"); }
- 01-28-2008, 03:51 PM #2
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
- 01-28-2008, 07:51 PM #3
For example
To add to jelly's reply, here's an example:
Java Code:import java.util.*; public class Main{ public static void main(String[] arg){ int number = 10; try{ number = addInteger(number, [COLOR="Red"]-5[/COLOR]); 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(); } }
Java Code:Error: positiveNumber cannot be negative
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
Similar Threads
-
Try Catch issue
By curtis_fraser in forum Advanced JavaReplies: 2Last Post: 12-13-2007, 11:04 PM -
Try Catch
By Renegade85 in forum New To JavaReplies: 4Last Post: 12-03-2007, 04:10 PM -
when to use try...catch
By javaplus in forum New To JavaReplies: 2Last Post: 11-18-2007, 08:52 PM -
try...catch block
By javaplus in forum New To JavaReplies: 3Last Post: 11-06-2007, 07:53 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:50 PM
Bookmarks