programming question about exceptions

  • Follow


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.
0
Reply paul.marquardt (4) 8/19/2003 3:25:57 PM

Paul M. wrote:
> 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.

0
Reply jacob9706 (365) 8/19/2003 3:41:24 PM


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.



"Jacob" <jacob@yahoo.com> wrote in message
news:3F424524.8040502@yahoo.com...
> Paul M. wrote:
> > 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.
>


0
Reply ajfish (393) 8/19/2003 4:27:57 PM

In article <f2137556.0308190725.49279030@posting.google.com>,
Paul M. <paul.marquardt@mortgagefamily.com> wrote:
>
>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
-- 
Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                    powered by emacs
0
Reply bcd (635) 8/19/2003 4:47:10 PM

3 Replies
37 Views

(page loaded in 0.082 seconds)

Similiar Articles:













7/26/2012 7:28:25 PM


Reply: