|
Yes, hence my statement, "cases you can't test for or don't expect to happen", I should have added, or happen often.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Okay. But why did you pick on the guy like this? He gave 3 alternate solutions, only one of which suggested catching the database exception. It seemed as if you were looking for a fight here
|
|
|
|
|
I wasn't "picking" on anyone nor did I criticize his perfectly valid solutions. His last response struck me as one who can't explain his position so comes back with playground response of "I know you are but what am I"
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Okay, but I just thought both of you (not just him) kinda went the "immature" route towards the end there.
|
|
|
|
|
Fair enough. Regardless, this is a good discussion.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: Regardless, this is a good discussion.
Oh yes, in fact I wish we had more of these in the Lounge
|
|
|
|
|
In principal you are correct. But remember that exceptions are there for exceptional cases; pun intented.
In the case of creating a new record where you expect the key to be unique, I agree with the others that using an exception is the correct approach. If there is a good chance that the key will not be unique then a different approach would be justified. There I would try to read the record with the key, if it does not exist then I would do the insert. I would however still have the try/catch on the insert.
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
|
|
|
|
|
Nagy Vilmos wrote: But remember that exceptions are there for exceptional cases
Yes, exactly for exceptional cases. I would use TryParse rather than Try/Catch but be prepared for UnhandledExceptions.
Nagy Vilmos wrote: If there is a good chance that the key will not be unique then a different approach would be justified. There I would try to read the record with the key, if it does not exist then I would do the insert. I would however still have the try/catch on the insert.
Agreed. But the Try/Catch is to catch unexpected cases not the normal flow.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: conditions that can be tested for
Do you check your car's battery charge, tire pressure, tread depth, lug nut torque, etc. every time you leave the house? Do you check your fly before leaving the men's room? Do you wear a belt and suspenders?
Just being able to perform a test, doesn't make it worthwhile.
|
|
|
|
|
If I expected my nuts to be loose I certainly would check my fly.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I'm not sure, but doesn't Sql Server throw a SqlException in these cases? The catch-all could hide conversion-errors, or index-out-of-bounds exceptions.
It's still best to try to be specific about the type of exception that you're trying to handle.
I are Troll
|
|
|
|
|
Yes only a SqlException is thrown, then you look at the number to determined the type of underlying Exception
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Eddy Vluggen wrote: I'm not sure, but doesn't Sql Server throw a SqlException in these cases? The catch-all could hide conversion-errors, or index-out-of-bounds exceptions.
Yeah, no one here has recommended a catch-all. What was recommended was to catch the specific exception thrown (which depends on what mechanism you are using to talk to the database).
My take on this is that there are no hard and fast rules for things like this. In general, I just try and stick to some consistent practices on a project and these practices themselves may not be identical across multiple projects.
|
|
|
|
|
Nishant Sivakumar wrote: Yeah, no one here has recommended a catch-all
Sorry, I get religious when I see one of those - it comes close to an "on whatevererror resume here".
Nishant Sivakumar wrote: My take on this is that there are no hard and fast rules for things like this
There's this article on CodeProject[^] that seems to suggest that it wouldn't cost much in terms of performance.
An exception that gets eaten OTOH, might be very costly.
Nishant Sivakumar wrote: I just try and stick to some consistent practices on a project and these practices themselves may not be identical across multiple projects.
True. Hell, I'll even color-code the exceptions if there's a compelling reason
I are Troll
|
|
|
|
|
Eddy Vluggen wrote:
Sorry, I get religious when I see one of those - it comes close to an "on whatevererror resume here".
Yep, absolutely agree.
|
|
|
|
|
Exactly, and your point is... ?
|
|
|
|
|
Programming by error - using an error in the normal flow of an operation. This is using the error trap is incorrect.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Welcome to the code project, what is your iternicene debate of choice today, sir?
|
|
|
|
|
Well, this has certainly been an interesting debate. What nobody has seemed to point out so far is that you actually should combine the two techniques. This is for a simple reason - while you should certainly attempt to detect the unique key violation explicitly, there is no guarantee that this alone will work. The reason for this is simple, presumably your application is going to be multi-user; well, between the time you check the uniqueness and the actual insert occurs, another user could have inputted the same values. So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition.
Simple. Job done.
|
|
|
|
|
As I responded prior, I would use TryParse rather than Try/Catch but be prepared for UnhandledExceptions
There is no one size fits all in software development and as the saying goes, they keep building better idiots.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
What does TryParse have to do with attempting to insert a duplicate key?
|
|
|
|
|
The statement is in regard to exception handling in general, which is what the debate is about, not a specific, narrow case of one type of SqlException
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Ah, thanks for clearing that up. I was a touch confused on this.
|
|
|
|
|
Pete O'Hanlon wrote: So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition.
I wouldn't do both if the exception handling is going to be necessary anyway. I see no advantage to doing that (e.g., performance would probably be better the exception route anyway). I sometimes see this strategy when dealing with multithreaded applications (e.g., you check a condition that is fast and commonly results in false, then you lock, then you check that condition again). However, I don't think that technique applies here, so I'd just go with the exception handling.
|
|
|
|
|
The reason for the first is simply that you are giving the user information up front that the item already exists. The try/catch is for the really exceptional case where there's a race condition.
|
|
|
|