• Post Reply Bookmark Topic Watch Topic
  • New Topic

Throwable  RSS feed

 
V Bose
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • 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...
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!