• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Throwing exception with an exception class?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a try/catch to throw an exception.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To continue from what Joanne said, there is a method designed to tell you whether a String is empty.
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic