This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Throwble a checked Exception

 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When we give :-

throw new Throwable();

If we do not either handle or declare the exception compilation fails.
So is Throwable a checked exception. What should be the answer to foll question :-


a) Throwable is a checked exception ? ( True or False)

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the definition of an unchecked exception ? If you know it, you'll know what type of exception Throwable is. Check the answer in the Java Specification.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The specification says : -


The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.


So, this means Throwable is a checked exception
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simran Dass wrote: So, this means Throwable is a checked exception

Yes that's right. You could've also created a simple program which throws an object of type Throwable and not catch it...
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Throwable class is the superclass of all errors and exceptions in the Java language


So it can be both checked and unchecked.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So it can be both checked and unchecked.

Hold on a second...
It isn't both checked and unchecked


The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.


is Throwable a RuntimeException or one of its subclasses? No
is Throwable an Error or one of Error's subclasses ? No

if you do as Ankit suggested and try to throw an unchecked throwable, it won't compile
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwable is super class of RuntimeException and Error so it contains them too, thats why when you throw a throwable you can catch it or not as per your wish.
If you dont catch a throwable compiler will not complain which is the case with RuntimeExceptions and Errors.

Throwable is superclass of all Exceptions, so it contains both checked and unchecked exceptions.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:when you throw a throwable you can catch it or not as per your wish.
If you dont catch a throwable compiler will not complain

Did you try it out??
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yow can't throw a Throwable without catching it. You can't say that it is both checked and unchecked exceptions
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I didn't try it and gave a wrong answer.
And i was a bit confused in my thoughts and I guess I wrote the other way round.

you guys are correct.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks

I had already tried it with code before posting the question but was
confused (same confusion as Neha's). My question contains -

throw new Throwable();

If we do not either handle or declare the exception compilation fails.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic