Results 1 to 20 of 27
- 08-04-2011, 04:03 AM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Question about Exception handling
Hello again,
so I'm trying to write a complete OOP in Java(yay...well not quite). So, here is the problem. I am trying to pick up good habits and think of exceptional events as I code, so I wrote the following and I'm trying to catch a couple of events. The problem is that I'm getting an error asking for an identifier and I have a simple question. Are they listed somewhere or do I make them up? Please see code below:
and I'm getting the following error:Java Code:try { Scanner userSelection = new Scanner(System.in); int userselection = userSelection.nextInt(); } catch (InputMismatchException) { System.out.print("You did not enter a integer.") } catch (NoSuchElementException) { System.out.println("There is nothing to read."); } catch (IllegalStateException) { System.out.println("This method has been invoked at the wrong time. Please try again when prompted."); }
So my questions:Java Code:----jGRASP exec: javac -g Numbers.java Numbers.java:29: error: <identifier> expected catch (InputMismatchException) ^ Numbers.java:33: error: <identifier> expected catch (NoSuchElementException) ^ Numbers.java:37: error: <identifier> expected catch (IllegalStateException) ^ 3 errors ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
Do I make up a letter or name?...well actually, I doubt it, because I just tried and received a "Cannot find Symbol" error.
Do I look them up somewhere? If so where? I went to the nextInt API and it can throw three exceptions, so I tried to catch them but no luck. Any direction would be appreciated.
Thanks.
- 08-04-2011, 04:11 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are just passing the class name of the exception into the catch blocks. You need to have a variable that's actually being caught
Java Code:catch(Exception e){/*...*/}
- 08-04-2011, 04:46 AM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Would you mind showing me an example from my example? I don't understand where your "e" comes from. I went back to the Java tutorial and nowhere in there do they declare the e.
- 08-04-2011, 04:50 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It's similar to how when a method is created you give it a formal parameter with the variable
Java Code:public class{ public example(int x){} //show you the handling of exception public static void main(String[] args){ try{ Integer.parseInt("a"); } catch(NumberFormatException nfe){ nfe.printStackTrace(); } } }
- 08-04-2011, 04:50 AM #5
You can use anything you like. The e in sunde's example is just like a parameter.
- 08-04-2011, 04:54 AM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
I"m still not getting where the nfe comes from? Could you have used abc instead of nfe for the NumberFormatException exception?
- 08-04-2011, 04:55 AM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 08-04-2011, 04:56 AM #8
Once again you can use anything you like. It is just the name of the parameter.
- 08-04-2011, 04:58 AM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Okay, thanks.
- 08-04-2011, 05:04 AM #10
You know how to write a main method?
Well guess what?Java Code:public static void main(String[] args)
All the above are valid and will compile.Java Code:public static void main(String[] foo) public static void main(String[] bar) public static void main(String[] stuff) public static void main(String[] anExtremelyLongAndPointlessNameSoWhyDidIChooseIt)
- 08-04-2011, 05:11 AM #11
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
So I just went and tried this:
and I am importing java.lang.Intger; and java.util.Scanner:Java Code:try { Scanner userSelection = new Scanner(System.in); int userselection = userSelection.nextInt(); } catch (InputMismatchException a) { System.out.print("You did not enter a integer."); } catch (NoSuchElementException b) { System.out.println("There is nothing to read."); } catch (IllegalStateException c) { System.out.println("This method has been invoked at the wrong time. Please try again when prompted."); }
and I'm getting this:
Why is That?Java Code:----jGRASP exec: javac -g Numbers.java Numbers.java:30: error: cannot find symbol catch (InputMismatchException a) ^ symbol: class InputMismatchException location: class LotteryNumbers Numbers.java:34: error: cannot find symbol catch (NoSuchElementException b) ^ symbol: class NoSuchElementException location: class Numbers 2 errors ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
- 08-04-2011, 05:13 AM #12
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 08-04-2011, 05:24 AM #13
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
did you import it?
import java.util.InputMismatchException;
- 08-04-2011, 05:30 AM #14
What makes you think those import statements will help with InputMismatchException and NoSuchElementException?
P.S. Catching runtime exceptions is generally frowned upon. If a runtime exception is thrown then it is an indication that there is a logic error in your code which should be corrected not just swallowed up with a catch statement.
P.P.S. You do not need to import anything from the lang package. It is imported implicitly for you.
- 08-04-2011, 05:35 AM #15
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 08-04-2011, 05:37 AM #16
You have to import any classes that are not in your current package (except lang as mentioned above). Exceptions are classes too.
- 08-04-2011, 05:39 AM #17
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
The short answer is ignorance? I saw that they were thrown in nextInt which is in Scanner so...I wrongly assumed that that would do...
I need to read other texts on exception. The Java tutorial does not connect with me.
I am not trying to swallow them, I'm trying to write the code so that if a user enters the wrong value, an error is displayed with the message that I have in the println("my made up message") method. Isn't that what an exception does?P.S. Catching runtime exceptions is generally frowned upon. If a runtime exception is thrown then it is an indication that there is a logic error in your code which should be corrected not just swallowed up with a catch statement.
Thank you for the reminder. :)P.P.S. You do not need to import anything from the lang package. It is imported implicitly for you.
- 08-04-2011, 05:42 AM #18
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
So just to be clear, everytime I try to catch an exception that could be possibly be thrown, I have to import it unless it is in java.lang.*;?
{complaint to the Java developers}. This is really not practical. This means that the code could end up with a bunch of import statements...
- 08-04-2011, 05:45 AM #19
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Oh, one more question. Since All exceptions are extend java.lang.throwable; which is within java.lang. Could I just import java.lang.*; everytime I have exceptions and be covered? Which makes me wonder, why do I have to import them in the first place if they are a sub-part of java.lang.* and it is automatically imported?
- 08-04-2011, 05:47 AM #20
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Similar Threads
-
Exception Handling
By link6790 in forum New To JavaReplies: 16Last Post: 05-19-2011, 06:57 PM -
Exception Handling
By eLancaster in forum New To JavaReplies: 4Last Post: 02-20-2011, 12:00 AM -
Handling Exception - elementary question
By kracer in forum New To JavaReplies: 2Last Post: 05-06-2010, 11:47 PM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks