Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

instanceof OR ClassCastException  RSS feed

 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering which of this approach is better:

or

I kinda prefer the second approach.
Thanks.
- sayta

[This message has been edited by Madhav Lakkapragada (edited June 19, 2001).]
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and why ?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO: exception catching shouldn't be used as a substitute for good programming. ie don't just catch an exception if you can test for the condition that would throw it first.
and that only cost me 2c
Dave.
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
you should use instanceof.
if you just try/catch, then it's more tryAndError than good programming.

karl
 
Yuri Gadow
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above posts hit it on the head, I would just add that you may find it helpful to think about what it means to the application if the element coming out of your vector is <em>not</em> an instanceof MySpecialClass.
In other words, is it an error that should not normally happen (use exception handling then) or is it a condition that will occur in normal operation (use instanceof).
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

well, looks like instanceof it is.
my reasons are simple (may be too absurd):
- I hate if else's
- I thought instanceof is a costlier (performance wise)
so i was curious to know if there is any better way.
Thanks everyone.
- satya
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should definately use instanceOf. There are two reasons for this.
1) Exceptions should be used for exceptional conditions. Not simply alternative path conditions that are not out of the ordinary during program execution.
2) InstanceOf is likely less expensive. While the instanceOf operation may not be cheap, I suspect it's cheaper than the cost of an exception. Remember that exceptions are first class objects. This means that when an exceptional condition is met, a new object must be created: memory allocated, fields initialized, constructors called, etc. This is expensive.
On a related note, don't do the following either:

as opposed to doing a for loop. It's the same idea.

For more information, see Peter Haggar's book [u]Practical Java[u], specifically "Praxis 24: Do not use exceptions for flow control."

Finaly, the best way to write poorly performing code is to say "I'll bet this is faster, so I'll go make the change." Developers, at all levels, are bad at guessing about timing (for various reasons, but that's another issue). If you every think one way is fater than another, be certain! Even if it's just a simple system call for the time before and after the code, it's better than nothing. More often than not, you end up doing more harm than good.

--Mark
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Mark.
- satya
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
instanceOf would be better.
but im not sure if it would be faster too.
sure, throwing an Exception brings in a new Object and it's overhead. but i assume that Exceptions are not that often (otherwise the code is poor).
So first checking with instanceOf and then casting is more expensive than only casting.
still: use instanceOf, dont abuse Exceptions........

karl
 
Tilak Ochani Jiwansingh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
First of all , it should be determined whether the exception would be caught frequently or not. If not then, I think, the try catch solution is better from performance point of you. Though it consumes the memory but it fastens the process and there is always trade-off between Memory Usage and Performance.
But if the code is suppose to throw exception very frrequently, then better to use instanceof operator.
I hope i was helpful
Tilak Ochani
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Tilak. What if the Vector is just intended as a collection of MySpecialClass objects, and know that the only way any other object could get into the Vector is if you've committed a programming error? You shouldn't have to use instanceof every time through the loop here - in this case a ClassCastError really does signal an exceptional condition, rather than and expected one, and so it's an entirely appropriate use of exception handling. But if you've designed the program so that the Vector is intended to hold other types of objects as well, then you should test with instanceof, as per most of the previous comments.
 
Wade Chandler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally speaking Exceptions "can" be expensive, but by nature aren't i.e. they should occur infrequently. From the code I can tell you mean to continue looping and not exit from the function or calling body. The reason Exceptions can be so expensive is that when an Exception is created it is usually populated by a call to Throwable.fillInStackTrace(). This can be minimized by turning off tracing in the VM, but that wouldn't be recommended. That tracing can be very helpful. I would recommend to anyone to use instanceof over Exceptions in code that looks like the above simply for that reason.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhav,
if one of your code examples caused a performance problem, I would simply try the other one and measure wether it solved it. If you don't have total control of the production environment, you might want to test this with different JDKs, as this might make a *big* difference.
If you don't have a performance problem, I would much more care about the *expressiveness* of the code. To me, the instanceof solution says "I am expecting different types of objects here, but only want to work on some of them"; whereas the exception solution says "if this isn't an XXX, something got terribly wrong".
If the latter is the case and the method calls are totally under control of my team, I'd even think about not handling the ClassCastException at all, but let it propagate. I don't like to hide programming errors - it tends to delay fixes...
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we have a grave digger here! This body ( thread ) was born June 19, 2001 01:49 PM and died peacefully on July 15, 2001 02:04 PM.
[ July 25, 2003: Message edited by: Jamie Robertson ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops.. :roll:
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, it depends on what you are expecting...
This seems related to iterating through a loop with

While it may make your code faster, it's a horrible idea unless needed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!