• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Checked Exceptions are Checked by Compiler to see if they are Handled or Declared

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'm randomly going over Sierra/Bates in preparation for the showdown in about two weeks....

I understand that Checked Exceptions are Checked by Compiler to see if they are Handled or Declared. But I cannot understand the following from Sierra/Bates, Chapter 5:

When an object of a subtype of Exception is thrown, it must be handled or declared. These objects are called checked exceptions, and include all exceptions except those that are subtypes of RuntimeException, which are unchecked exceptions. Be ready to spot methods that don’t follow the “handle or declare” rule, such as



You need to recognize that this code won’t compile. If you try, you’ll get

MyException.java:3: unreported exception MyException; must be caught or declared to be thrown doStuff();
^
Notice that someMethod() fails to either handle or declare the exception that can be thrown by doStuff().




This doesn't make sense. I thought the checked exception was declared with



and it gets handled in the catch statement




Help!

 
Ranch Hand
Posts: 83
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:

^
Notice that someMethod() fails to either handle or declare the exception that can be thrown by doStuff().




so even someMethod() needs to be declared as "void someMethod() throws MyException" as it has a method doStuff that throws exception..
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy cow, never mind.......


catch clause in doStuff throws an exception, but who on Earth is going to catch it? someMethod, that's who! However, someMethod fails declare that it throws MyException, nor does it place the method call doStuff() in a try/catch block

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rikesh Desai wrote:

Sandra Bachan wrote:

^
Notice that someMethod() fails to either handle or declare the exception that can be thrown by doStuff().




so even someMethod() needs to be declared as "void someMethod() throws MyException" as it has a method doStuff that throws exception..




LOL, I missed by one minute.....
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exactly!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic