Results 1 to 3 of 3

Thread: try catch!?

  1. #1
    Joe2003 is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default 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");
    }

  2. #2
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    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

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default 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();
    	}
    }
    will output
    Java Code:
    Error: positiveNumber cannot be negative
    Hope that helped you. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Try Catch issue
    By curtis_fraser in forum Advanced Java
    Replies: 2
    Last Post: 12-14-2007, 12:04 AM
  2. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 05:10 PM
  3. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 09:52 PM
  4. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 08:53 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08: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
  •