Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree1Likes

Thread: Question about Exception handling

  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default 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:

    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.");
    		}
    and I'm getting the following error:

    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.
    So my questions:

    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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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){/*...*/}

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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();
        }
      }
     }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    You can use anything you like. The e in sunde's example is just like a parameter.

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    I"m still not getting where the nfe comes from? Could you have used abc instead of nfe for the NumberFormatException exception?

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    You can use anything you like. The e in sunde's example is just like a parameter.
    Oh okay.

    So after using a method, I can look up its exception, catch it by writing:

    catch (NameOfException trivialLetters) {
    do something
    }

    Is this correct?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Once again you can use anything you like. It is just the name of the parameter.

  9. #9
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Okay, thanks.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    You know how to write a main method?
    Java Code:
    public static void main(String[] args)
    Well guess what?
    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)
    All the above are valid and will compile.
    Fubarable likes this.

  11. #11
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    So I just went and tried this:

    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 am importing java.lang.Intger; and java.util.Scanner:

    and I'm getting this:

    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.
    Why is That?

  12. #12
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    You know how to write a main method?
    Java Code:
    public static void main(String[] args)
    Well guess what?
    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)
    All the above are valid and will compile.
    Thanks. That part I get. My problem is not with my method, it's with the exception. I have two questions and you elucidated the one about the name, but I still need to understand why I am getting this error after making the suggested corrections.

  13. #13
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    did you import it?

    import java.util.InputMismatchException;

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Quote Originally Posted by bigsonny View Post
    and I am importing java.lang.Intger; and java.util.Scanner:
    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.

  15. #15
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by yellowledbet View Post
    did you import it?

    import java.util.InputMismatchException;
    I didn't realize that I had to input exceptions. I'll try that.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    You have to import any classes that are not in your current package (except lang as mentioned above). Exceptions are classes too.

  17. #17
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    What makes you think those import statements will help with InputMismatchException and NoSuchElementException?
    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.

    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.
    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.P.S. You do not need to import anything from the lang package. It is imported implicitly for you.
    Thank you for the reminder. :)

  18. #18
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    You have to import any classes that are not in your current package (except lang as mentioned above). Exceptions are classes too.
    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...

  19. #19
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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?

  20. #20
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Thank you all for your help. The final code works with the import statements for each exception and I'm now going to read up on exceptions again.

Page 1 of 2 12 LastLast

Similar Threads

  1. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 06:57 PM
  2. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:00 AM
  3. Handling Exception - elementary question
    By kracer in forum New To Java
    Replies: 2
    Last Post: 05-06-2010, 11:47 PM
  4. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM

Posting Permissions

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