• Post Reply Bookmark Topic Watch Topic
  • New Topic

throw exceptions?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




hi guy's I'm doing a project heres the code I came up with so far but I'm having trouble with the exception part it does not do what I want it to do,when a user enters lets just say a letter I want it to throw an exception and print out the a message but not only does it print out a message it then says below it an exception in thread "main" and then it goes onto to give me more errors saying at java.util.... on five different lines beneath this

I don't want that to happen all I want it to do is print out "you can not enter a letter" instead of that huge error message

how could I fix this?

thanks
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are all sorts of other things to say about the code, but you are making problems for yourself by rethrowing the Exception. Let's see what is going on.
  • 1: You are getting input via a Scanner object.
  • 2: You can avoid getting Exceptions altogether; go through my posts and search for Scanner and hasNextInt and you will find lots and lots of posts, but some will be helpful.
  • 3: You are catching plain simple Exception, which is not usually a good idea. You shou‍ld usually catch something more specific.
  • 4: You are now throwing plain simple Exception out of the method, and th compiler won't like that. Not without a throws clause.
  • I would suggest your best bet is to get rid of the exceptions altogether. I searched my old posts; I think the discussion in this thread is too long for you just at the moment. This post will be easier to read, but I recommend you change the method so the Scanner is a private final static field of the class, not a parameter to the method.

    For information about why you have to declare throwing an Exception, try the Java™ Tutorials.
     
    Tobias Bachert
    Ranch Hand
    Posts: 86
    18
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A small correctur regarding point 4)
    A throws clause is not required as the compiler can ensure that e has to be a RuntimeException (due to the fact that only RuntimeExceptions may be thrown within the try-block and e is rethrown without intermediate assignments etc.).
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch TB. Your point appears to be correct.
     
    Liutauras Vilda
    Sheriff
    Posts: 4923
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Apart from the main topic.

    Could you explain please, what role "alarm" variable is meant to play in your code?
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:Apart from the main topic.

    Could you explain please, what role "alarm" variable is meant to play in your code?


    thanks guys,

    and the role o the alarm would a  hypotheticalalarm that would sound when the tempature is too high.
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's just that the variable alarm is not used in the program.  This tends to create warnings.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!