I have no idea where you came up with my post saying that try/catch blocks should not be used.
D'Arcy, it's the only conclusion that I can draw from the position in your first post.
exceptions should be confined to...things that aren't expected in code.
I have no problem and actually support try/catch blocks within code.
Given that one catches exceptions and not something else, and that one can't catch an exception that one doesn't expect, espousal of the first statement constitutes repudiation of the second. Hence my first post was based on the conclusion that you did NOT support the second statement. Now, you say you do, without retracting your support of the first. So, with all due respect, what's up with that? Do my logic or the assumptions upon which it is based have a hole somewhere?
The other thing I didn't understand, and still don't, is why exception handling should be avoided with some sorts of exceptions (anticipated ones), and not with others (unanticipated ones). Let me use the three examples you give to clarify what I don't understand:
if a query is executed and no records are returned but an object needs to be passed back, there is no reason to throw an exception: the developer can code in anticipation of that scenario and, it should be pointed out, that result is not an error; its a possible scenario within the app.
Why is it about this scenario that precludes throwing an exception? Why is another methodology indicated? Why isn't it best to have a consistent approach across the board? The way that I would handle this is by throwing an exception (why am I wrong to do so?) from the provider of the data to the consumer. A developer coding "in anticipation of that scenario" would simply catch the exception thrown. Isn't "coding in anticipation" exactly what catching exceptions is?
let's say that someone pulls the plug on the SQL Server and the code can't open the connection. This is where catching a SQL error can be performed. However, that doesn't mean that the exception can't be handled in the calling code without throwing any extra exceptions. Again, coding in anticipation.
This kind of illustrates my point that this is the way everyone else is doing it. SQL Server throws any number of different exceptions when it can't open the connection. For example, if there's an invalid user or password, SQL Server will throw an exception. So, it would appear that the designers of the SQL Server provider are perfectly happy to throw exceptions for all sorts of reasons, trusting the consumer to catch them and handle them in its own way. Why is this a bad idea in your mind? (Or do you not think this is a bad idea, and what am I missing about your point of view?)
Now let's say that there's bad data in the database, and when the object tries to fill itself, and type-mismatch exception is thrown. Well, this is definatley exceptional: why is there bad data in the database? This should be logged, communicated, and possibly bubbled up to the user and the program halted.
So, why is this a good reason to throw an exception, and your first example not a good reason? You're giving an example here of the developer coding "in anticipation of the scenario", too, aren't you? I don't see that you're drawing any substantive distinction between these two scenarios. (All I can see is that you're saying that one is an error, and the other one isn't. If so, then what's the difference and why is it relevant?)
Now, D'Arcy, I'm not just trying to pick your posts apart for the sake of doing it. When someone says something that I disagree with (strongly in the case of your first post) I try to find out why the person feels the way he or she does, with the idea of learning more. So, no, you haven't cleared up your stance on the issue to me. I'm less clear on it than I was before, in fact (although probably closer to what it really is, too). I do have an open mind, though, and want to understand what you're trying to tell me. Would you mind taking another crack at it?
Thanks,
Bob