Multiple Exception Definitions in One File

K

kvnsmnsn

In Ada I could have a package that consisted of a group of exception
declarations, and any compilation unit that "withed" in that package
could use them. On the other hand if I want to use my own customized
exceptions in Java, it looks like I have to declare each one individu-
ally in its own file. Is this really what I have to do, or is there a
way to declare a bunch of exceptions in one file so that I can use
them elsewhere?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
T

Thomas Fritsch

In Ada I could have a package that consisted of a group of exception
declarations, and any compilation unit that "withed" in that package
could use them. On the other hand if I want to use my own customized
exceptions in Java, it looks like I have to declare each one individu-
ally in its own file. Is this really what I have to do, or is there a
way to declare a bunch of exceptions in one file so that I can use
them elsewhere?

---Kevin Simonson

You can put more than one class into one compilation unit. But then only 1
class may be public:
// MyException.java
public MyException extends ... { ... }
class MySecondException extends ... { ... }
class MyThirdException extends ... { ... }

With the trick of inner classes you can have more than one public class per
compilation unit:
// MyException.java
public MyException extends ... {
public static class SpecialException extends ... { ... }
public static class AnotherException extends ... { ... }
}
// use like: throw new MyException.SpecialException();
 
K

kvnsmnsn

Thomas Fritsch posted:

=You can put more than one class into one compilation unit. But then
only 1
= class may be public:
= // MyException.java
= public MyException extends ... { ... }
= class MySecondException extends ... { ... }
= class MyThirdException extends ... { ... }
=
=With the trick of inner classes you can have more than one public
class per
= compilation unit:
= // MyException.java
= public MyException extends ... {
= public static class SpecialException extends ... { ... }
= public static class AnotherException extends ... { ... }
=}
=
=// use like: throw new MyException.SpecialException() ;

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
{}

public class ExcepTwo extends Exception
{}
}
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>
 
T

Thomas Fritsch

[...]
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top