Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Throwable

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

Compiles and runs fine.
If the catch clause @ Line 1 is replaced with any of Throwable's subclasses, predictably, a compiler error along the lines of 'This type of Exception is never thrown in the body of the try statement'...
What's the reasoning behind the 'Throwable' in the catch clause working...
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi V Bose,
Thats because Throwable includes Runtime and Checked exceptions both. If you look at API it will tell that,
Throwable is parent of Exception,Error and in Error API it says,

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

which means the code in try catch can throw Error even w/o making methods called in that block as "throws Error" you know.
So, when we write catch Throwable it assumes that we want to probably catch some Error which occurs at runtime. If you write Exception then it means it could be checked exception and in that case it goes and see if there is a possibility of getting any checked exception in the code or not. Hence it doesn't compile if there is no possbility of getting checked exception in the try catch block.
Hope it helps.
Maulin
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic