programming question about exceptions

P

Paul M.

Hi,
Two questions, actually.

1. I have a class Graph which defines this method:

public void addVertex( Vertex someVertex ) {...
The list to which it adds is a private Vector() of Vertex objects.
There is no other public access method to add vertices to a Graph
instance. I can't think of ways that an object of a different type can
end up in the list. I wrote the code-- doesn't mean there aren't any
bugs.

I have another class:
public Vector getVertices() {...

This returns the Vector of Vertex object. When I cycle through the
members of the vector and use them, I cast them as such:
Enumeration enumVertices = graphObject.getVertices().elements();
while( enumVertices.hasNext() ) {
Vertex v = (Vertex) enumVertices.nextElement();
v.doSomethingVeryImportantForManKind();
}

Is there any compelling reason to put the line
Vertex v = (Vertex)...
inside of a try/catch loop and catch a ClassCastException? I can't
think of one, as the the Vector is private and the addVertex method
controls how Objects are added to the Vector. I am using the code in a
GUI and also plan to run
it on very large Graphs, testing for isomorphism, etc. outside of the
GUI.


Question 2 (or is it 3...):
What's a good source, paper or online, to answer questions like this,
especially concerning robustness of code and efficiency. I'm not a
formally-trained programmer-- came up through the ranks of perl (still
love/do it) and I'm used to exception handling along the lines of do {
this(); } or die;

Thanks for your help.

Paul M.
 
J

Jacob

Paul said:
Hi,
Two questions, actually.

1. I have a class Graph which defines this method:

public void addVertex( Vertex someVertex ) {...
The list to which it adds is a private Vector() of Vertex objects.
There is no other public access method to add vertices to a Graph
instance. I can't think of ways that an object of a different type can
end up in the list. I wrote the code-- doesn't mean there aren't any
bugs.

I have another class:
public Vector getVertices() {...

This returns the Vector of Vertex object. When I cycle through the
members of the vector and use them, I cast them as such:
Enumeration enumVertices = graphObject.getVertices().elements();
while( enumVertices.hasNext() ) {
Vertex v = (Vertex) enumVertices.nextElement();
v.doSomethingVeryImportantForManKind();
}

Is there any compelling reason to put the line
Vertex v = (Vertex)...
inside of a try/catch loop and catch a ClassCastException?

No. None!

What whould you do in the catch block anyway?
If it happens that other than a Vertex sneaked
into your vector, a program crash is probably
the preferred outcome.
 
A

Andy Fish

I'm definitely with jacob on this one.

one of the best things about exceptions is what happens when you don't catch
them. java tells you exactly where it went wrong

One of the worst things you can do is catch exceptions and then just use
that to throw a new exception all the time. It is somewhat tempting if you
are used to checking return codes, but is counterproductive as it spoils the
stack trace.
 
B

Bent C Dalager

I have another class:
public Vector getVertices() {...

This returns the Vector of Vertex object. When I cycle through the
members of the vector and use them, I cast them as such:
Enumeration enumVertices = graphObject.getVertices().elements();
while( enumVertices.hasNext() ) {
Vertex v = (Vertex) enumVertices.nextElement();
v.doSomethingVeryImportantForManKind();
}

If you're not supposed to be able to edit the returned list, I'd
consider using List in stead of Vector and implementing the method
something like the following:

public List getVertices() { return Collections.unmodifiableList(vertices); }
where "vertices" is your
private Vector vertices;

If you did this, then there is no chance at all that anyone outside of
your class can add non-Vertex objects to your private vector.

(Well, they _might_ be able to if they used reflection to access
private members, but then they bloody well deserve to have the program
break on them.)

Cheers
Bent D
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top