• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help me to understand compiletime & runtimr exceptions

 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
so far i have been thinking

JVM thrown exceptions - Runtime exceptions
Programatically thrown - Compiletime excetions

but it is proved wrong..

how to understand this without looking into API.

Or

memorizing important exceptions type is the only way.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no such thing as compile-time exceptions; they're compile-time errors, which are shown by the compiler to indicate syntax errors in the source code.

Runtime exceptions can be thrown by either the JVM (or rather, the class libraries that ship with the JVM) or your own code.
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if so what is checked exceptions.

the reson for my query is:
i am not able to understand overriding concept which states
'overriding methods should not throw new checked exceptions or broader exceptions whereas it can throw any new unchecked exception or narrower exceptions or no exceptions.

[ March 25, 2008: Message edited by: m prabhu ]
[ March 25, 2008: Message edited by: m prabhu ]
 
Kesu sundar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by m prabhu:
'overriding methods should not throw new checked exceptions or broader exceptions whereas it can throw any new unchecked exception or narrower exceptions or no exceptions.


This means that
if a base class method throws an an exception, and when we override the same in the sub class (method signature should be same as per rule), we have the following choices

1. use the same exception in the overrided method
2. use a subclass of base class exception
3. or no exception
4. throw an unchecked (runtime) exception.


bcos as exception propagates up in the reverse order ( from subclasses to base..) we cant have a new checked exception or a more generalised exception than base class.
[ March 25, 2008: Message edited by: Kesu sun ]
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can understand the overriding rule.

what i am not understanding is how to know
-this is a unchecked exception and checked exception
 
Kesu sundar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything extending from RuntimeException class is an unchecked exception.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A checked exception extends java.lang.Exception. The Java compiler requires these to be declared to be thrown, or handled by a try/catch statement.

Unchecked exceptions extend java.lang.RuntimeException, and are not required to be handled by the code.

You can read the javadocs for the RuntimeException and Exception classes for more information on how to use each one.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read just above the screenshot of the below post with same heading as below-:
How do I know if it is a checked exception or an unchecked exception?
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thank you for everybody.
especially the link is very useful.

thank you.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ulf. Its all you need to know for SCJP 5.0. BTW I didnt encounter a single question to differentiate b/w checked & unchecked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic