Frisky said:
At this point, I think we have beaten this horse to death.
Perhaps. It looks like I'm not going to persuade you that exceptions
really *aren't* a performance problem in reasonable real world apps,
and you're not going to persuade me that they are...
However, we have not yet achieved the maximum number of posts, the maximuim
depth on a post, or maximum number of bytes in a reply either. So, nntp on
dude ...
Well, this set of posts will be the last for the night from me - it's
already past midnight here, and I'm sure my little boy will be getting
me up in about 6 hours...
It does not make it any less viable a tactic. For resources though, I am
with you. That was purely based on need. As explained.
After prodding though - it was still the solution you suggested to the
OP
Funny, but I just use the methods that throw when I need that, and the
methods that return null when I need that. Neither is better than the other.
They just have different uses.
That's because you dislike exceptions though - when using exceptions is
the normal, idiomatic way of showing a very unexpected situation (such
as a resource being missing), they look very different.
Actually, I was not giving advice on an application. The questions was,
(para-phrased) files have been added to the project, but I don't know how to
access them. My code suficed this answer and showed some quasi
implementation. And with the message "does this help?" I think it answers
the question fine. (Other than my mistake of grabbing only part of the code
out of a huge utility class in an attempt to be quick.)
In my experience, people tend to use code they're presented with,
whether it's actually suitable or not, unfortunately.
Depends on the application. If we are sticking to the resource theme, most
applications would not have had as much code as we've written to this
newsgroup in them.

Much less have a need to worry over the triviality of
whether to throw versus check for null. Which kind of makes this post
senseless at some level. The reality is, for resources, either method in an
application like the "generic" one you describe would not matter because, if
there is an issue, you are going to get it during development. Once you have
it working, its a "who cares".
Absolutely - and using exceptions, you end up with much more readable
code. In particular, you just need:
public static Image RetrieveResourceImage (Assembly assembly,
string resourceName)
{
Assembly entryAssembly = assembly.GetEntryAssembly();
using (Stream stream = entryAssembly.GetManifestResourceStream
(resourceName))
{
return Image.FromStream(stream);
}
}
No need for two extra methods which do nothing but unnecessary error
handling.
(Looking at that code now, it's not right - you're meant to keep the
stream open for the lifetime of the image, which doesn't happen in the
above; it doesn't happen in your code either though.)
But, that does not change the fact that one or a thousand, throwing is much
much slower, and uses more resources than checking for null.
Actually in this case it's not, because you're not stopping the
exception from being thrown in the first place - you're just catching
it and changing to a different error-handling solution. If higher up
the chain you decide to change back to using an exception, you've
actually just doubled the number of exceptions which need to be
thrown...
Regardless, if you're really worried about the ~0.01ms for the single
exception which is likely to halt processing of that request or
whatever anyway, I think using a platform like .NET is a bad idea
anyway. Do you avoid ever creating objects unless you absolutely have
to as well, whether or not it leads to less readable code?
Uh, today. I ran your sample. And mine a couple of days before that.
Touche.
Actually, I don't have that problem, because I use them where I need to, and
use checks where I need to.
So you haven't run into it as an actual problem, but you're adding lots
and lots of code doing error checking in order to avoid this supposed
problem?
You did run your test twice didn't you? So that it had already been JITed.
JITting happens every time you run the app, not just once (unless you
use Ngen).
I don't know where you get 100,000,000 throws = a half a second.
I never said that I did. I said I got 100,000,000 checks for nullity in
half a second - reread the post.
Mine, in release mode was over 1 and 1/2 seconds for 100,000. And
while my box ain't the latest, its plenty fast. It is a simple
statement. Exceptions are slower. Use them wisely.
"Premature optimization is the root of all evil (or at least most of
it) in programming." - Donald Knuth.
I'm throwing exceptions merrily and avoiding lots of return value
checking. I can hand-on-heart say that I've *never* found exceptions to
be a performance problem. It sounds like you haven't except in the very
strange situation you wrote about before. Now, doesn't avoiding
exceptions sound like premature optimisation?
Release or debug, exceptions are still slower. Dramtically slower.
Yup, but not nearly as bad as they look in debug. And they're
sufficiently fast that I just don't care, so long as my program doesn't
have the kind of problem that means I'm throwing tens of thousands of
them a minute - in which case I've got far bigger fish to fry.
Suppose my app *were* throwing 10,000 exceptions per minute, and let's
say my computer could only throw 50,000 exceptions per second (instead
of the 89,000 it can actually do.)
That still means that it would only be taking up about 0.3% of the
processor time *even in a situation where I would be very, very
concerned for other reasons*.
When looking for performance optimisations, I look for things which
will gain me an order of magnitude, or at the *very, very* least
increase performance by more than 1% (usually much more).
Out of interest, how many apps which use exceptions freely like I do
really suffer a *significant* performance problem due to it?
I know exactly what gave me null when I ask for it.
Only if you either check for null immediately. What if you forget, and
pass the parameter to a method which stashes it for a while, or perhaps
doesn't even throw an exception, but takes a different course?
I know exactly what asserted when I asserted it.
Do you have assertions turned on in the field? If so, that's costing
you performance *when things are working*. The ability to throw
exceptions is basically free when things are working. I'd rather have
code that performs slightly worse in the debugger but slightly better
in the field than vice versa.
A stack trace is not something I want to resort to unless there is a
real problem at hand. When my application throws an exception, I
don't normally go look at the stack trace. I look at where the
debugger was when it threw.
That's because you don't use exceptions as your preferred event
handling mechanism though. I'm quite prepared to see exceptions in the
diagnostic logs for real world apps,
To each his own. Such is life.
I've been having to read a load of C code that deals with errors in
what I view as the old-fashioned way. I then come back to me C# and
Java code and thank goodness for exceptions which can make life sane
again...
Ah, my point comes through. Use them judiciously I say.
And I've never said otherwise - but I view your avoidance of exceptions
on performance grounds as unjudicious, if there is such a word. It
seems to be based on a bogey-man which no-one ever really sees - the
app which *does* have performance problems due to over-use of
exceptions.
And yes, I am sure you could say it could go the other. That is why I use
both.
And that is why I recommend the right one for the right use. (But we will
probably never get consensus on the "right use" or the "right one".
We certainly won't if you keep arguing against their use due to
performance reasons.