🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #3575

Re: Vector puzzlement

#3575
From: Brian Burton
Date: Thu, 06 Nov 1997 00:00
34 lines
1430 bytes
Greg Hamer wrote:
> 
> You are on the right track. You just need to cast it back to a DataClass
> object .
> For safety's sake you can say
> Object o = e.nextElement()
> if (o instanceof DataClass){
>   DataClass dc = (DataClass) o;
>   //do stuff
> ...
> }
> 

You're much better off without the instanceof test.  Go ahead and do the cast in
a try block and catch any ClassCastException that might get thrown.  That way
the general case (cast successful) works faster (avoids the redundant instanceof
test) and the exceptional (probably never encountered) case is handled
gracefully.

try {
  DataClass dc = (DataClass) o;
  // do stuff
} catch (ClassCastException e) {
  // oops .. put error handling code here
}

All the best,
++Brian

-- 
Brian Burton                        Custom Software Development
Burton Computer Corporation         Java, C++, Orbix, Tuxedo
http://www.burton-computer.com      OO Consulting and Mentoring
Technical Details
Message-ID: <346221CF.BD1F6692@burton-computer.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <VA.0000001c.14bfc5b5@p5> <344BF601.C8D0B3D1@borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,210eeb670cddf2cd
X-Google-Attributes: gid1089d0,public
From: Brian Burton <brian.burton@burton-computer.com>
Subject: Re: Vector puzzlement
Date: 1997/11/06
Message-ID: <346221CF.BD1F6692@burton-computer.com>
X-Deja-AN: 287485878
References: <VA.0000001c.14bfc5b5@p5> <344BF601.C8D0B3D1@borland.com>
Organization: Burton Computer Corporation
Newsgroups: borland.public.jbuilder.java.api