Results 1 to 6 of 6
  1. #1
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Multiple exception catch block

    Hi, I get value1Exception cannot be resolved to a type! & value2Exception cannot be resolved to a type!
    Why is that? I also changed both to only catch(Exception ex), but still the same! Any help would be appreciated...


    Java Code:
    public static boolean validateInput(String value1,String value2,String value3){
    		boolean isValid = false;
    				
    		try {
    			int num1 = Integer.parseInt(value1);
    			if(!withinRange(num1,MIN_NUMBER, MAX_PRIME)) {
                  isValid = true; 
    	         
    		 
    		
    		try {
    			int num2 = Integer.parseInt(value2);
    			if(!withinRange(num2,MIN_NUMBER, MAX_PRIME)) {
                  isValid = true; 
    	         
    			}
    		}
    		
    			 catch(value1Exception ex) { 
    				 System.out.println("The entered value " + value1 + " is not a valid integer.  Please try again.");
    	         }
    		}
    	}
    		catch(value2Exception ex) { 
    			 System.out.println("The entered value " + value2 + " is not a valid integer.  Please try again.");
    			
            }
    		
    		
    	        return isValid;
    	}

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Multiple exception catch block

    where did you define value1Exception and value2Exception?

  3. #3
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Multiple exception catch block

    I didin't bcoz Exeption originaly doesn't need and didn't know I have to! Thank you :)
    do I have to create a new method? or I can do it right here?

  4. #4
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Multiple exception catch block

    i'm not sure as to what you want to do with that, if you want to define a new exception then you need to write a class that extends Exception,
    and maybe even use the throw keyword, depends on what it is you want.
    for example:
    Custom Exception Example

  5. #5
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Multiple exception catch block

    i fixed it, but prints my errors 3 times, what should i do?

    Java Code:
    public class test {
    	
    	// Global Constants
        final static int MIN_NUMBER = 1;
        final static int MAX_PRIME = 10000;
        final static int MAX_FACTORIAL = 12;
        final static int MAX_LEAPYEAR = 4000;
      
        //Global Variable
        static int a,b,c;
    	
    	
    public static void main (String[] args) {
    		//String[] myNumbers= new String [3];
    			
    		//int x =Integer.parseInt(args[0]);
    		for(int i =0; i< args.length; i++){
    			//System.out.print(args[i]+" ");
    			validateInput(args[0],args[1],args[2]);
    			//performOperations(args[0],args[1],args[2]);
    			
    		}
    	}
    			
    					
    //Validate User Input
    	public static boolean validateInput(String value1,String value2,String value3){
    		boolean isValid = false;
    				
    		try{
    			try{
    				try{
    			a = Integer.parseInt(value1);
    				if(!withinRange(a,MIN_NUMBER, MAX_PRIME)) {
    					 System.out.println("The entered value " + value1 +" is out of range [1 TO 10000].");
    			}
    				isValid = true; 		
    				
    			}
    			catch(Exception ex) { 
    				 System.out.println("The entered value " + value1 + " is not a valid integer.  Please try again.");
    					}	
    		
    			b = Integer.parseInt(value2);
    				if(!withinRange(b,MIN_NUMBER, MAX_FACTORIAL)) {
    					System.out.println("The entered value " + value2 +" is out of range [1 TO 12].");
    		}
    				isValid = true;	
    			}
    	catch(Exception ex) { 
    				System.out.println("The entered value " + value2 + " is not a valid integer.  Please try again.");
    }
    		
    			c = Integer.parseInt(value3);
    			if(!withinRange(c,MIN_NUMBER, MAX_LEAPYEAR)) {
    				System.out.println("The entered value " + value3 +" is out of range [1 TO 4000].");
    	}
    			isValid = true;	
    		}
    catch(Exception ex) { 
    			System.out.println("The entered value " + value3 + " is not a valid integer.  Please try again.");
    }	
    			
    	        return isValid;
    		
    	        
    	}

  6. #6
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

Similar Threads

  1. Try Catch Block
    By Jackbower in forum New To Java
    Replies: 3
    Last Post: 05-06-2012, 07:00 AM
  2. try catch block issue
    By srivempala in forum Advanced Java
    Replies: 29
    Last Post: 07-22-2011, 06:45 PM
  3. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 02:32 PM
  4. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •