This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extending from Exception

 
Sharada Kumaran
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a restriction on extending from Exception? The following code gives me an error - "Incompatible type"
However, if I extende MyException from RuntimeException or any other Exception, it compiles fine.
What am I missing here?
public class Exception1{
public void divByZero() throws MyException
{
int num1 = 10;
int num2 = 0;
if ( num2 == 0) throw new MyException("dividing by zero");
}
public static void main(String args[])
{
try{
new Exception1().divByZero();
}
catch(MyException myEx){System.out.println("catching ");}
finally{System.out.println("finally in main");}
}
}
class MyException extends Exception{
MyException(String msg){
super(msg);
}
}
 
sona gold
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i tried running the program
it did not give any error
i am sure there is something ur doing wrong while compiling
please check ur program correctly
are u saving the file as Exception1.java
or check some other small mistake that u might be doing
the program is correct, there r no errors in it
 
Sharada Kumaran
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the identical code, in the same file called Exception1.java
No luck! Like I mentioned before, if I replace
class MyException extends Exception
with
class MyException extends RuntimeException //or any other exception
I don't get any error.
Hope to get this mystery solved.
 
jeena jose
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
program is running fine
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sharada,
Your code compiles and runs correctly under JDK 1.3. What compiler are you using?
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Sharada Kumaran
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using jdk1.3 too. The prog doesn't run. And the funny thing is that there is no problem when I extend from any other Exception, like RuntimeException, InterruptedException.
The exact error I get at each line MyException is found is -
Exception1.java:6: incompatible types
found : MyException
required: java.lang.Throwable
and I also get -
.\Exception.java:16: incompatible types
found : Exception
required: java.lang.Throwable
catch(Exception e) {
Does this throw any clue?
Since you are not getting any errors, I shall assume that there is no problem extending from Exception, and proceed for my certification. Meanwhile, I still wonder why I am getting an exeption here.
thanks,
Sharada
 
Michael Zhao
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it complies and run fine under jdk1.3 in linux
catching
finally in main
but something in your code it is a typo or?
Exception1.java:6: incompatible types
found : MyException
required: java.lang.Throwable
and I also get -
.\Exception.java:16: incompatible types
found : Exception
required: java.lang.Throwable
catch(Exception e) {
why it is Exception.java:16: ?
and catch(Exception e) { ?
can not see it in your code.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sharada,
It looks as if the compiler is finding another Exception class before it finds the one in the JDK bin directory. Have you got another Exception.class file somewhere in your classpath? Maybe another piece of example code you were playing with?
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited May 27, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic