Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is an exception necessary in this case?

 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, if I want the program to simply stop if a user enters a string beginning with "n," do I need to throw an exception, and if so, how do I do that?
I read the oracle tutorial on exceptions but I'm still really confused.

Also, how should I make it so that the method runs regardless of whether the user types a capital or lowercase letter?
Thanks!

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We do not have complete information here.

There are various ways for a Java program to stop. Throwing an exception does not, necessarily, stop the program, and throwing one is a separate subject from stopping.

An exception usually indicates something unexpected happening; some program(mer)s also use it to indicate that something that has gone for some period of time has now ended (like reaching the end of a file that's being read).

So you could throw an exception out of this method, but whether that stops your program depends on the method that is calling this one, and you haven't shown us that. THAT method would either have to catch the exception and end the program, or not catch the exception and have it caught (or not) by a method further up the calling tree.

The two "normal" ways of terminating a Java program are 1. falling out of the bottom of the main() method, 2. calling System.exit(). If you just want to stop everything right here, calling System.exit() will do it. I (and some other programmers) consider it a somewhat extreme method of ending an application of any size, because usually you don't know at every point in the program whether there are other things the program would like to do before it exits, and it is better to give other parts of your program a chance to react to the fact that things are terminating so they can do what they want. But for simple programs and/or example code, it will suffice.

I will be happy to answer questions about exceptions, but prefer to start with one or more questions about them; otherwise I end up writing a tutorial that will likely be just as enlightening as the one you've already read.

You can use the String method equalsIgnoreCase() to determine if two strings are equal except for case, or you can convert unknown input to a known case with toUpperCase() or toLowerCase() and compare only to the known case.

rc
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually Exceptions are good in situations like:
* A file should be in a place that but it is not
* A conection that has failed
* Some bad data that could break your code and it should never get there.

I think in your case your could get the typed value and decide if you will process it or not.
I think there is no reason to thrown an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic