aspose file tools*
The moose likes Beginning Java and the fly likes simple Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple Exception handling" Watch "simple Exception handling" New topic
Author

simple Exception handling

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
This is my first study into Exception handling. Why doesn't this code work? I've commented the part that I'd hoped would behave differently. Is it not possible to perform logical tests within the catch block?
Thank You



[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Without trying to run the code I can say that you need to get into the catch block first. And the only way of getting into the catch block to throw the corresponding exception.
So in your case if you are not throwing "NumberFormatException" than you will never get into the catch block of code.


Please ignore post, I have no idea what I am talking about.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Dirk,
Your error has nothing to do with Exception handling. You must have come from a C/C++ background to have made this error. Don't worry because it is probably the error that most beginning java programmers make.
When dealing with Strings in java the == operator doesn't work like you expect it to. The == operator just compares memory locations. In your case, the memory locations are different therefore you always go through your else case.
If you want to actually compare String contents then you must use the equals method. For example:
inputTextField.getText() == "one"
will always be false. But
inputTextField.getText().equals( "one" )
might be true or false depending on the inputTextField value.
It is the small things that cause the most problems and are the hardest to debug without stepping back and looking at the big picture. Another set of eyes is a great thing to have ...
Regards,
Manfred.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Thank You Manfred!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple Exception handling
 
Similar Threads
Applet using RMI
Applet notinitited, please need urgent help
Applet servlet communication
Help netbeans saying No main class found
Problem with GUI