Re: Multiple Exception Definitions in One File



<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('$','@')


.



Relevant Pages

  • Re: Marshaling Delegates as Struct/Class Members
    ... >I understand delegates do not need to be pinned, but its the class object ... > public class TestObject ... > value, GCHandleType type) ... > Is there an easy way to marshal delegates as part of a struct? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Marshaling Delegates as Struct/Class Members
    ... >>I understand delegates do not need to be pinned, but its the class object ... >> public class TestObject ... >> value, GCHandleType type) ... >> Is there an easy way to marshal delegates as part of a struct? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Errors when deleting a row in a JTable.
    ... > The code below return a good class object but I've already the same stack ... > public Class getColumnClass(int columnIndex) { ... or TYPE of the wrapper class for some other simple type. ...
    (comp.lang.java)
  • Re: Protected inner classes and inheritance
    ... sharper3@xxxxxxxxxxxxxxxxxxxxx (Scott Harper) wrote: ... If I simply make TopLevel.Inner a public class, ... I have to make *everything* in the Inner class public to ... Also, if I move the SecondLevel class into the package somePackage, I can keep ...
    (comp.lang.java.programmer)
  • Re: Protected inner classes and inheritance
    ... If I simply make TopLevel.Inner a public class, ... I have to make *everything* in the Inner class public to ... Inner is only accessible to members of the same package or any subclass. ...
    (comp.lang.java.programmer)