Results 1 to 6 of 6
Thread: Multiple exception catch block
- 10-17-2012, 07:03 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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; }
- 10-17-2012, 09:11 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
Re: Multiple exception catch block
where did you define value1Exception and value2Exception?
- 10-17-2012, 06:31 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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?
- 10-17-2012, 07:32 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
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
- 10-17-2012, 11:45 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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; }
- 10-18-2012, 12:11 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Try Catch Block
By Jackbower in forum New To JavaReplies: 3Last Post: 05-06-2012, 07:00 AM -
try catch block issue
By srivempala in forum Advanced JavaReplies: 29Last Post: 07-22-2011, 06:45 PM -
Try/catch block
By swati.jyoti in forum New To JavaReplies: 5Last Post: 07-02-2009, 02:32 PM -
Question reg try/catch block
By nn12 in forum New To JavaReplies: 1Last Post: 09-16-2008, 05:56 PM -
try...catch block
By javaplus in forum New To JavaReplies: 3Last Post: 11-06-2007, 07:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks