• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

extending from Exception

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
program is running fine
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic