• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception - Catch  RSS feed

 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
[code]
class AQ {
public static void main(String args[]) {
System.out.println("Before Try");
try {
} catch(IOException t) { // here
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
[code]
The above code gives a compilation error saying that the exception is "never thrown" . Agreed.
But if the code in that line is made to catch "Throwable"
} catch(Throwable t) { // here
it compiles fine.
Why does it doesnt give a compiler error.... bcoz even in this case the Throwable can never be thrown....
Can anyone help me understand this...
Thanks in advance.
Aruna
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aruna,
Compiler does not compalin about Throwable 'coz this SUPER CLASS never throweN by JVM. Check the the JDK explanation...
"The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or of one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause."
Hope this helps...
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Sivaram said, exceptions extend the Throwable class. So, wherever an exception object can be used, it can be generalized and a Throwable object can be used instead. This is also true for classes in the Exception hierarchy. Actually you an call a method that throws an IOException , lets say, and then catch just the 'Exception'. Since IOException is a subclass of Exception, it works fine.
Hope that helps,
Ajith
[This message has been edited by Ajith Kallambella (edited December 22, 2000).]
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction Ajith, Throwable is not an Interface it's a class derived from Object.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha!
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sivaram & Ajith.
I got it now.
Aruna
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Aru,
I check you following code:
=====================================
class AQ {
public static void main(String args[]) {
System.out.println("Before Try");
try {
} catch(IOException t) { // here
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
===================================
When I run the above code on my machine (Win 98, PI-200, JDK1.2.1) I get following error: class IOException not found. Contradict to you're error message "that the error is never thrown".
Then I replaced the class file name inside catch parantheses with Throwable (super class of Exception & Error), with Exception , with Error, with RuntimeException( subclass of Exception), with SecurityException (subclass of RuntimeException) and the code compiled/run without any problem. I tried to locate but could not find IOException file (as subclass of Throwable at any level).
On further investigation I found out that IOException is in java.io package. Then by modifying you're code by adding import java.io.IOException; the code on compile time gave me following error: "Exception java.io.IOException is never thrown in the body of the corresponding try statement."
Intresting point to note here is that java.io.IOException is aslo a subclass of java.lang.Exception. But IOException being in the different package behave in this way.
I think if only classes could have "protected" access modifiers then IOException would have worked fine.
=============================================
Can some one please explain more clearly that "why subclasses of Exception declared in packages other than lang package give error at compile time."
============================================
Thanks,
Regards,
Raj.

 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Aru,
Sorry, I would like to correct my explanation give about the way exceptions arre handled in Java. I did some reserch word from old posts and got the following information. I am wrtting down those things and hope things will be clearer for you. First just think what could be the possible answer of this question in true and false.
JVM will not throw any exceptions. Only programs will throw exceptions.
Exceptions are always thrown by the JVM, usually in response to something in the program. But the program isn't necessarily the cause. Example - if the program is reading data through a connection across the internet, the read() method is declared with a "throws IOException". If I then reach over to the phone line that connects my computer to the internet, and disconnect it, then the JVM will throw an IOException of some sort. Did the program cause this, or did I? I'd say that I did.
All subclasses of RuntimeException (which is an Exception) and Error are constructed and thrown by JVM at appropriate places, when need comes.
All checked exceptions are checked by the compiler whether they are caught properly. Otherwise compile time error occurs. Therefor all subclasses of Exception (excluding RuntimeException), irrespective of in which package they are (as i was explaining earlier), will give error message at compile time if no method/code is written in try block that throw that exception, and you have provided a catch block for it (that is subclass of Exception excluding RuntimeException).
Both Exception and Error can also be thrown by programs through throw new aSubClassOfThrowable(); statement.
If the program throws a checked exception(all subclasses of class Exception excluding RuntimeException ) they must be enclosed by try..catch/try..finally/try..catch..finally statements ...or they must be declared in the throws clause of the appropriate method(s).
Hope things are much clearer now.
Regards,
Rajpal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!