Re: Multiple Exception Definitions in One File
- From: "Thomas Fritsch" <i.dont.like.spam@xxxxxxxxxxx>
- Date: Sun, 29 May 2005 23:29:40 +0200
<kvnsmnsn@xxxxxxxxxxx> wrote in message
news:1117336514.926360.215600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[...]
>
> With this advice I built the following two classes, <Exceps> and
> <UseExceps>, but when I tried to compile them I got the following er-
> ror messages. Thomas, aren't I doing what you suggested I do? If you
> or anyone else can see what I'm doing wrong here, I'd really appreci-
> ate it if you'd let me know what it is.
>
> ---Kevin Simonson
>
> "You'll never get to heaven, or even to LA,
> if you don't believe there's a way."
> from _Why Not_
>
> ####################################################################
>
> kevin@linux:~/Cs677/Lab3> cat Exceps.java
> public class Exceps
> {
> public class ExcepOne extends Exception
You missed the *static* key-word here (as given in my first reply). It is
essential!
public static class ExcepOne extends Exception
> {}
>
> public class ExcepTwo extends Exception
same as above ...
> {}
> }
> kevin@linux:~/Cs677/Lab3> cat UseExceps.java
> public class UseExceps
> {
> public static void main ( String[] arguments)
> {
> int ite;
> double value, sum = 0.0;
>
> try
> { if (arguments.length == 0)
> { throw new Exceps.ExcepOne();
> }
> for (ite = 0; ite < arguments.length; ite++)
> { value = Double.parseDouble( arguments[ ite]);
> if (value == 0.0)
> { throw new Exceps.ExcepTwo();
> }
> sum += 1.0 / value;
> }
> System.out.println
> ( "Average of the reciprocals is " + (sum / arguments.length) +
> '.');
> }
> catch (Exceps.ExcepOne excptn)
> { System.err.println( "ExcepOne thrown.");
> }
> catch (Exceps.ExcepTwo excptn)
> { System.err.println( "ExcepTwo thrown.");
> }
> }
> }
> kevin@linux:~/Cs677/Lab3> javac Exceps.java
> kevin@linux:~/Cs677/Lab3> javac UseExceps.java
> UseExceps.java:10: an enclosing instance that contains Exceps.ExcepOne
> is required
> { throw new Exceps.ExcepOne();
> ^
> UseExceps.java:15: an enclosing instance that contains Exceps.ExcepTwo
> is required
> { throw new Exceps.ExcepTwo();
> ^
> 2 errors
> kevin@linux:~/Cs677/Lab3>
>
Some background on inner classes:
Non-static inner class objects have a reference to their enclosing outer
class object.
Therefore you create instances of the inner class like:
OuterClass outerObject = ...;
Object innerObject = outerObject.new OuterClass.InnerClass();
This is why your compiler complained "an enclosing instance is required".
Static inner class object don't have a reference to an enclosing outer class
object.
Therefore you can create instances of the inner class like:
Object innerObject = new OuterClass.InnerClass();
This is what you need for your exceptions.
I must concess that this is quite a difficult topic. More info can be found
in Language Spec,
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#262890
(But be warned: it is a difficult there, too)
--
"TFritsch$t-online:de".replace(':','.').replace('$','@')
.
- Follow-Ups:
- Re: Multiple Exception Definitions in One File
- From: kvnsmnsn
- Re: Multiple Exception Definitions in One File
- References:
- Multiple Exception Definitions in One File
- From: kvnsmnsn
- Re: Multiple Exception Definitions in One File
- From: Thomas Fritsch
- Re: Multiple Exception Definitions in One File
- From: kvnsmnsn
- Multiple Exception Definitions in One File
- Prev by Date: Re: AccessControlException
- Next by Date: Re: Java vs. C++
- Previous by thread: Re: Multiple Exception Definitions in One File
- Next by thread: Re: Multiple Exception Definitions in One File
- Index(es):
Relevant Pages
|