permaculture playing cards*
The moose likes Java in General and the fly likes 'throws' clause specified; exception not thrown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Watch " New topic
Author

'throws' clause specified; exception not thrown

Hilary Mann
Greenhorn

Joined: Jun 07, 2012
Posts: 13
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Dec 10, 2009
Posts: 65

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:



OCPJP6
Hilary Mann
Greenhorn

Joined: Jun 07, 2012
Posts: 13
Got it :



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

@myyron latorilla, I meant the opposite...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'throws' clause specified; exception not thrown