Make my own Exception?

J

Joan

I am reading a file and checking each piece of data.
If there is something wrong with the data, I do this:

throw new IOException("Cell type not NUMBER at row = " + ix........");

How does one decide if this is the sort of thing that
should have it's own Exception like BadDataException() ?
 
S

Stefan Schulz

I am reading a file and checking each piece of data.
If there is something wrong with the data, I do this:

throw new IOException("Cell type not NUMBER at row = " + ix........");

How does one decide if this is the sort of thing that
should have it's own Exception like BadDataException() ?

IMHO you rarely have too many exception classes, provided they inherit in
a sane manner from common classes.
 
S

Steve W. Jackson

Joan said:
I am reading a file and checking each piece of data.
If there is something wrong with the data, I do this:

throw new IOException("Cell type not NUMBER at row = " + ix........");

How does one decide if this is the sort of thing that
should have it's own Exception like BadDataException() ?

An Exception is a class like any other. So the decision should likely
be based on the same kinds of criteria you used to decide when to create
your own classes that extend existing class definitions.

= Steve =
 
R

Rivky

You should also factor in if you would like to handle that error
differently than a normal IO exception. In other words, if you want to
handle that type of error differently than you would a normal IO
Exception, you should probably create a specific exception class and
handle/catch it.
 
A

Andrew McDonagh

Joan said:
I am reading a file and checking each piece of data.
If there is something wrong with the data, I do this:

throw new IOException("Cell type not NUMBER at row = " + ix........");

How does one decide if this is the sort of thing that
should have it's own Exception like BadDataException() ?

The main problem for reusing rather than creating new exception classes,
comes when we need to handle the exceptions differently.

For example, how would the calling code be able to differentiate between
the IOException stated above, and any other IOException - like
FileNotFoundException, etc. ?

By reusing a single exception type for multiple reasons, we lose the
class type differences, which prevents having specific catch blocks and
so specific handlers.

Just today, I was developing a Model class that data could be
Added/Edited/Removed from.

The model throws exceptions when:
1) adding of data if the identifier has already been used,
2) editing data when the identifier does not exist.
3) Removing data when the identifier does not exist.

all three methods throw specific kinds of exceptions to show the intent
of their interface, rather than a generic ModelException. However, all
exceptions do extend an Abstract ModelException, so callers have a
choice of handling the explicit exception from each method, or just the
generic intermediate class.


for example...


class Model {
void add(Id id, Object data) throws IdentifierAlreadyExistsException {
if (contents.containsKey(id)){
throw new IdentifierAlreadyExistsException (id);
}
...
}

void edit(Id id, Object data) throws NonExistantIdentifierException {
if (! contents.containsKey(id)){
throw new NonExistantIdentifierException (id);
}

}

...
}


class abstract ModelException extends Exception (

ModelException(ID id, String message) {
super(message);
this.id = id;
}

Id getId() {
return id;
}

private Id id;
}

class NonExistantIdentifierException extends ModelException {

NonExistantIdentifierException(Id id) {
super(id, "no data for the supplied Identifier exists");
}
}


class IdentifierAlreadyExistsException extends ModelException {

IdentifierAlreadyExistsException(Id id) {
super(id, "data for the supplied Identifier already Exists");
}
}
 
J

Joan

Andrew McDonagh said:
The main problem for reusing rather than creating new exception classes,
comes when we need to handle the exceptions differently.

For example, how would the calling code be able to differentiate between
the IOException stated above, and any other IOException - like
FileNotFoundException, etc. ?

I could look at the string that is returned, but that is extremely ugly and
I would rather make a new one.

The stuff below is very clear and totally usefull, thanks.
 
A

Andrew McDonagh

Joan said:
I could look at the string that is returned, but that is extremely ugly and
I would rather make a new one.

agreed, its a case of: why bother inventing your own runtime type (i.e.
the string) when we have an easy means of creating a compile time type
(the derived class).

The stuff below is very clear and totally usefull, thanks.

glad to help.

Andrew
 
R

Roedy Green

How does one decide if this is the sort of thing that
should have it's own Exception like BadDataException() ?

if ever you want to treat them differently from IOExceptions when in
your catching then they deed a different name.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top