This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes extending from Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "extending from Exception" Watch "extending from Exception" New topic
Author

extending from Exception

Sharada Kumaran
Greenhorn

Joined: May 23, 2001
Posts: 29
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);
}
}

Sharon
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
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


sona<br />SCJP
Sharada Kumaran
Greenhorn

Joined: May 23, 2001
Posts: 29
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

Joined: May 06, 2001
Posts: 69
program is running fine
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Sharada Kumaran
Greenhorn

Joined: May 23, 2001
Posts: 29
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

Joined: Sep 19, 2000
Posts: 77
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.


SCJP,SCWCD,SCMAD,OCP9I, SCEA
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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).]
 
 
subject: extending from Exception
 
Similar Threads
try-catch problem
Is finally, really finally
What's different between throw, throws and try catch
what is the concept behind?
Exception thrown from catch and finally block...