• Post Reply Bookmark Topic Watch Topic
  • New Topic

Throwing exception with an exception class?  RSS feed

 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, so I got this program. It takes input from the user and assigns it to fields on an object. But, it's meant to check the users input. If the user enters bad input, it's supposed to throw this exception. For each of these exceptions, theres a class specifically for it.



And this is the exception class.





It's just meant to tell the user that they entered an invalid value, which would mean if they entered an empty string instead of a name.

I've never really been good at exception handling, so it's quite confusing to me.
 
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
You don't need a try/catch to throw an exception.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you intend line 41 to do ?
At the moment it is trying to call a static method of the InvalidNameException class and that method does not exist.
If you tell us what you wanted that line to do, we may be able to help.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further to Knute's comment not needing a try/catch when throwing an exception, your newName if statement should be using the "equals" method rather than ==
 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Further to Knute's comment not needing a try/catch when throwing an exception, your newName if statement should be using the "equals" method rather than ==


Right. So what would be a proper way for it to determine if the string doesn't have any characters/is empty?
 
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
You're really not reading the posts. Use the equals method.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W Wilson wrote:
K. Tsang wrote:Further to Knute's comment not needing a try/catch when throwing an exception, your newName if statement should be using the "equals" method rather than ==


Right. So what would be a proper way for it to determine if the string doesn't have any characters/is empty?

You can still compare it to "", you just need to use the equals method instead of ==, but there is actually a method designed for this purpose. Take a look at the javadoc for the String class.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To continue from what Joanne said, there is a method designed to tell you whether a String is empty.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may also be useful to clarify what is an empty String.

Is a String containing just spaces such as "     " to be treated the same as the String of ""?
If so, you will want to also look in the String API for a method that could trim white spaces before checking if the String is empty.

(I left some easter eggs to help you find the names of those methods in that last sentence)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!