aspose file tools*
The moose likes Beginning Java and the fly likes java.lang.Exception is checked or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.Exception is checked or not?" Watch "java.lang.Exception is checked or not?" New topic
Author

java.lang.Exception is checked or not?

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 408

I have on query regarding exceptions, basically people consider java.lang.Exception as a checked exception. If RuntimeException itself is a subclass of Exception,then why Exception is checked and RuntimeException is unchecked? Please provide the explanation and if java.lang.Exception is a checked exception then why we don't need to handle the following at compile time??

public static void main(String...args) throws Exception{
//do some stuff........O'reilly
}


Pawan Chopra
SCJP - DuMmIeS mInD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
If you have got SCJP you must have forgotten what you learned for the exam.

The details are all in the Java Tutorials.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3216
A RuntimeException class represents exceptions that occur within the Java virtual machine (during runtime). An example of a runtime exception is NullPointerException. The cost of checking for the runtime exception often outweighs the benefit of catching it. Attempting to catch or specify all of them all the time would make your code unreadable and unmaintainable. The compiler allows runtime exceptions to go uncaught and unspecified. If you like, you can catch these exceptions just like other exceptions. However, you do not have to declare it in your �throws" clause or catch it in your catch clause. In addition, you can create your own RuntimeException subclasses and this approach is probably preferred at times because checked exceptions can complicate method signatures and can be difficult to follow.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 408

Campbell Ritchie
If you have got SCJP you must have forgotten what you learned for the exam.


Well I know Its really bad that I forgot this if it was written anywhere in kathy seira's book.I didnt see this in that book or may be I didnt read it properly(Generally the worst part is my aim was to just clear the ceritification). Anyways thanks for reminding me that I should have knowledge of these things because I am an SCJP .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Anyway, have you found out from the link I gave you whether java.lang.Exception is checked or not? The API for Exception doesn't tell you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
And sorry for being rude about the exam.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 408

Its all right , there should be someone for people like me to realize them the truth!!

Anyways, I found this from that tutorial:

All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses.


And the class java.lang.Exception comes under exceptions category in API!!

Please explain what is true? Is it checked?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
The Java Language Specification doesn't tell you a lot more.
Throwable: checked.
Error: unchecked.
All sorts of error including ThreadDeath and AssertionError: unchecked.
Exception: checked.
RuntimeException: it says in the API link that is is unchecked and gives a list of subclasses.
Subclasses of Exception as on its API page: checked, except for RuntimeException.
Your own Exceptions: If they have RuntimeException as a superclass, directly or indirectly: unchecked, otherwise unchecked.
[ November 03, 2008: Message edited by: Campbell Ritchie ]
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 408

Thanks for the confirmation!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
You're welcome and sorry for being rude earlier.
 
 
subject: java.lang.Exception is checked or not?
 
Similar Threads
Exception doubt
java.lang.Exception not a checked exception?
Exceptions : Help Needed
Checked Exception.
Checked exception and Runtime exception