• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a User Defined Exception  RSS feed

 
Larissa Perkins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am only nearing the end of my first Java programming class... so please keep that in mind when explaining things.


I know that I am not 100% comprehending try/catch blocks, but after scouring message boards, forums, and Oracle, I still can't pick out where I am going wrong.

I have a ValidateInput class where I am trying to check that a String only has letters. If not, then throw an exception message via JOptionPane. I created my own NonLetterException class. When I call the method containing the try/catch Eclipse gives me an Unhandled Exception Type error.

in main()


in ValidateInput





Aren't I handling it in the try/catch? What did I miss?

Also, I have have tried the NonLetterException class as nested in ValidatedInput, but also not nested. To me nested makes more sense. Can anyone tell me if this is a better way to go? I have never nested classes before, but it makes sense to me because I am not using this exception in other parts of my program.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared that your method throws NonLetterException.
The compiler does not care whether the method can actually throw the exception. You have declared it might throw one so you need to handle it whenever you call the method (in your case in main).
Another option is to remove throws clause as it seems unnecessary.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your main() method have a "throws NonLetterException" clause, or a catch block to handle this exception? If not, then the Java compiler complains that your main() method is not handling the exception properly.

For checked exceptions, in any method where the exception might occur, you have to either catch it or specify that the method can throw this kind of exception by adding a "throws" clause to the method declaration.

Your stringInput method catches the exception, but it also has a "throws NonLetterException", so if you call this method from main(), then the compiler thinks that your main() method must also handle the exception. Either handle it in main() or remove the "throws NonLetterException" from the method stringInput.
 
Larissa Perkins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took out the throws declaration because both of you were right... they were unnecessary. I was mistakenly thinking that I needed to add that to any method that would have a try/catch block throwing that exception.

Thank you so much for both of your help.

My NonLetterException class is telling me "the serializable class does not declare a static final serialVersionUID field of type long". Looking online, I still can't find what this means. My program is running, but for further learning I would like to know why I am getting this.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
 
Larissa Perkins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pawel. I found that page before I posted that last reply but I must have missed the reply in the middle of the page which just cleared it up for me.

Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!