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: Result of Exception in constructor and destructor !! - comp.lang ...As for your second question, you should never throw an exception from a destructor, because the ... Using thread-specific data in shared libraries - comp.programming does vector::resize throw bad_alloc exception? - comp.lang.c++ ...... bad_alloc from the allocator (I thought the question was about bad_alloc, not about exceptions in ... does vector::resize throw bad_alloc exception? Programming ANN: Seed7 Release 2009-12-06 - comp.programming... Seed7: seed7_05_20091206.tgz In the Seed7 programming ... Post Question | Groups ... that moving to an existing file raises an exception. Fortran Question - comp.lang.fortran[Question] Install gcc -Arithmetic Exception (core dumped ... segmentation fault (SIGSEGV ... Fortran Programming Interview Questions and Answers Fortran Programming ... FAR CALL/JMP to an absolute address - comp.lang.asm.x86... Group, I am fairly new to the realm of assembly programming and wanted to ask a few fundamental questions ... Not surprisingly, I got an Access violation exception. May be ... Enable alignement exception on x64 (under Windows x64) - comp.lang ...... Post Question | Groups ... OR crash on Itanium), I want enable the exception for ... Programmer's Manual Volume 2: System Programming.) bad pointer exception - comp.lang.c++Although it seems so, your question has nothing to do ... one of these people who gets excited about "programming ... bad pointer exception - comp.lang.c++ std::exception ... detecting disconnection with select() - comp.unix.programmer ...> Is a socket disconnection treated as an exception? No ... Linux: detecting disconnection with select() - programming ... programming.itags.org: Unix & Linux question ... C++: PTHREAD_CANCEL_ASYNCHRONOUS and random crash - comp ...... Post Question | Groups ... saying "terminate called without an active exception". ... comp.programming.threads 557 articles. 1 followers. can acronym change indefinite article? - comp.text.texThere may be some exceptions to these. Nevertheless, the ... 0800, gazzar wrote: > I couldn't find this question ... to handle it automatically, but my (La)TeX programming ... Exceptions - Java Programming Questions and AnswersThis is the java programming questions and answers section on "Exceptions" with explanation for various interview, competitive examination and entrance test. Solved ... Exception Handling - C# Programming Questions and Answers Page 2This is the c# programming questions and answers section on "Exception Handling" with explanation for various interview, competitive examination and entrance test. 7/26/2012 7:28:25 PM
|