aspose file tools*
The moose likes Beginning Java and the fly likes Is an exception necessary in this case? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is an exception necessary in this case?" Watch "Is an exception necessary in this case?" New topic
Author

Is an exception necessary in this case?

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
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

Joined: May 29, 2005
Posts: 479
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

Joined: Jul 14, 2010
Posts: 754

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.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
 
Consider Paul's rocket mass heater.
 
subject: Is an exception necessary in this case?