Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'throws' clause specified; exception not thrown

 
Hilary Mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through my buddy's old SCJP notes and found
"When a method is declared to throw exception, it must throw it; else compiler will force you to do so."

I did not understand and this code runs fine:



Did I misinterpret, or is there some other scenario (like try-catch)?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UnsupportedOperationException is a RuntimeException and you are not forced to handle in the invoking method. Try to change it to Exception and compiler will ask you to handle it.
 
Myyron Murray Latorilla
Ranch Hand
Posts: 65
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think what it means is if your code for example has and explicit throw like:



then your method signature should indicate the thrown Exception like:

 
Hilary Mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it :



@John Jai, 'Exception' is still not working! How?

@myyron latorilla, I meant the opposite...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic