• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using throws Exception I'm getting an error  RSS feed

 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use the throws clause, I get an error that states, unreported exception java.lang.exception must be caught or declared to be thrown..

I do catch it in Main so idk what the actual problem is.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is telling you exactly what's wrong: If method A() calls method B(), and B() can throw a checked exception, than A() has to either catch that exception, or declare that it throws it, so that A()'s caller knows what kinds of things can go wrong.

Google for java exception tutorial for more details.
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am catching it in main.. I call it in main, Try.. l1.setLeft.. and then I catch the error. Am I still doing something wrong?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler's not lying. Which line is it complaining about?
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, dang it. Its being used in another method. Now I see what you meant. I thought I was calling it for the first time in Main.. This is an assignment and the teacher had written a class for us and we are supposed to enhance it. I didn't even see that. Plus we are supposed to write everything in notepad for the first few assignments so I kept overlooking the actual line of error.. Thanks a lot for the help!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I found it:



Since setLeft() can throw Exception, Line's c'tor has to either catch it or declare it. I go with declaring it. After all, if setLeft fails, you don't have a valid Line object.

Also, throwing and catching plain ol' Exception is a bad idea. Use one more appropriate to the specific problem. Define your own Exception subclass if the existing ones don't fit your needs. In this case, I would just use IllegalArgumentException. (Note that this exception is unchecked, so you don't have to catch or declare it. It's the appropriate one though, since somebody calling setLeft with an invalid value is a bug, and it's not your code's job to deal with it.
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:.
In this case, I would just use IllegalArgumentException. (Note that this exception is unchecked, so you don't have to catch or declare it. It's the appropriate one though, since somebody calling setLeft with an invalid value is a bug, and it's not your code's job to deal with it.


Will do, thanks a lot for the quick response and help. Def saved me some agony.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!