RuntimeException or Error or what?

H

HK

Suppose a class needs data from a resource, i.e. some
file alongside in the .jar with the class file. This
data needs to be read only ever once, because it
is not about to change and is not supposed to change.

The place to read the resource (I think) is
in the static initializer of the class.

The result of getResourceAsStream() may nevertheless turn
out to be null. One reason could be that someone unpacked
the .jar and then the resource "disappeared".

The class has no way to operate properly without the data;
someone could as well have deleted the class file itself.
What is the proper Exception to throw:

a) RuntimeException (or subclass),
b) Error (or subclass),
c) something else?

Thanks,
Harald.
 
M

Michael Rauscher

HK said:
Suppose a class needs data from a resource, i.e. some
file alongside in the .jar with the class file. This
data needs to be read only ever once, because it
is not about to change and is not supposed to change.

The place to read the resource (I think) is
in the static initializer of the class.

The result of getResourceAsStream() may nevertheless turn
out to be null. One reason could be that someone unpacked
the .jar and then the resource "disappeared".

The class has no way to operate properly without the data;
someone could as well have deleted the class file itself.
What is the proper Exception to throw:

a) RuntimeException (or subclass),

Since the problem is a missing resource, I'd throw a
MissingResourceException.

Bye
Michael
 
T

Thomas Hawtin

HK said:
Suppose a class needs data from a resource, i.e. some
file alongside in the .jar with the class file. This
data needs to be read only ever once, because it
is not about to change and is not supposed to change.

The place to read the resource (I think) is
in the static initializer of the class.

The result of getResourceAsStream() may nevertheless turn
out to be null. One reason could be that someone unpacked
the .jar and then the resource "disappeared".

The class has no way to operate properly without the data;
someone could as well have deleted the class file itself.
What is the proper Exception to throw:

a) RuntimeException (or subclass),
b) Error (or subclass),
c) something else?

Just Error. Or perhaps InternalError. The usual thing when you get an
impossible situation.

Are you sure you want to stop the class from loading? Handling
exceptions from failed class loading, other than the class named with
forName, is witchcraft. JUnit tries to access a file from a static
intialiser, which makes it impossible to adapt the runners to a secure
environment without modifying the classes..

It also smells of global variables and difficulty of configuration.

OTOH, if you consider missing the resource to be a linkage error, go
ahead and throw an error.

Tom Hawtin
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top