M
Mark Sizzler
As far as I know one should not use the operator "instanceof" if possible.
Why?
Mark
Why?
Mark
Mark Sizzler said:As far as I know one should not use the operator "instanceof" if possible.
Why?
[...]xarax said:I only use "instanceof" when I am not the author
of the classes for which I am testing. If I am
the author, then I can use built-in down-casting
by the JVM for free. If the classes are "foreign",
then I'm stuck using "instanceof".
Example: I am catching an IOException, but I want
to test for a particular subclass to know what
exactly went wrong. I didn't author the classes nor
did I instantiate the reference, so I must use
"instanceof".
[...]xarax said:I only use "instanceof" when I am not the author
of the classes for which I am testing. If I am
the author, then I can use built-in down-casting
by the JVM for free. If the classes are "foreign",
then I'm stuck using "instanceof".
Example: I am catching an IOException, but I want
to test for a particular subclass to know what
exactly went wrong. I didn't author the classes nor
did I instantiate the reference, so I must use
"instanceof".
Why not use multiple catch blocks? That's why they're available.
In message said:[...]
Why not use multiple catch blocks? That's why they're available.
Also, I fail to see how this would be affected by whether or not
the exception-throwing code is under my control...?
Please set the Followup-To header when crossposting.
[...]Maybe this example helps? You can replace the call to foo()
with a method the exception-throwing code of which you do not
control (as long as you know that it can throw an EOFException).
[...]In message said:Maybe this example helps? You can replace the call to foo()
with a method the exception-throwing code of which you do not
control (as long as you know that it can throw an EOFException).
And exactly how would you do this differently if foo() _was_ under
your control?
I would declare all the thrown exceptions in the trows clause
of the method. If the method never throws a pure IOException
I propably would not include it in the throws clause.
But that was not the point I was going after. I was showing a
way to do the exception handling without using instanceof.
I only use "instanceof" when I am not the author
of the classes for which I am testing. If I am
the author, then I can use built-in down-casting
by the JVM for free. If the classes are "foreign",
then I'm stuck using "instanceof".
Why not use multiple catch blocks? That's why they're available.
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.