|
at least they left you a message to let you know what should happen... I've seen them not even try and catch an exception.
|
|
|
|
|
catching and doing nothing is actually worse. It completely hides the fact that anything went wrong. Which uually means you crash somewhere else later.
|
|
|
|
|
Well there are lots of cases where catching and doing nothing is ok.
|
|
|
|
|
Add TODO: to the comment, so Visual Studio can remind you something still needs to be improved.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Reminds me of Resume Next from a particularly dark past!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Reminds me of Resume Next from a particularly dark past!
Somewhat, but it's easier to limit the effect of catch-and-ignore to a single statement. Realistically, there are quite a few circumstances in which if an error happens very often the developer should be made aware of it, but beyond that there's no particularly useful action the code can take in response to an error.
For example, if a field that affects the display of a control is changed outside the UI thread, it's necessary to use BeginInvoke to let the control update itself. If the BeginInvoke works, great. If it fails because someone closed a window at just the "right" time, so what? It's important to ensure that the code doesn't waste time repeatedly trying to BeginInvoke a control after it's disposed, but otherwise occasional invocation failures should be expected and I really don't see anything useful to be done with them other than ignore them.
|
|
|
|
|
supercat9 wrote: Somewhat, but it's easier to limit the effect of catch-and-ignore to a single statement. Realistically, there are quite a few circumstances in which if an error happens very often the developer should be made aware of it, but beyond that there's no particularly useful action the code can take in response to an error.
In my app, this is the rare case. I think I have one or two and I commented like this
catch(Exception e)
{
// nothing we can do about an error here
}
Unfortunately, for a lot of people, exception handling consists of
catch(...)
{
}
Somewhere they learned this is the proper way to handle exceptions. If it prevents the app from crashing (at this point anyway), that's all you need to do. If you blow up a buffer, hide it and let the next guy deal with it.
|
|
|
|
|
If something like an empty try/catch is discovered in code that I am in charge of (and believe me, I will discover it, thanks to FxCop), I will refuse to accept it for production code.
If an error happens and absolutely no action can or should be taken (a very rare case), then at least the exception has to be logged and there has to be a comment in the code that explains why it's OK to just "swallow" the exception in this special case. This is the very least for good code...
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Thomas Weller wrote: If something like an empty try/catch is discovered in code that I am in charge of (and believe me, I will discover it, thanks to FxCop), I will refuse to accept it for production code.
If an error happens and absolutely no action can or should be taken (a very rare case), then at least the exception has to be logged and there has to be a comment in the code that explains why it's OK to just "swallow" the exception in this special case. This is the very least for good code...
Regards
Thomas
All I can add is AMEN
I will confess that in my code above where I "swallowed" the exception was when a thread was ending and his parent had already disappeared. Which in practice, has never happened. I believe that is the only time I have done this.
I sent out an email a few years ago telling people to add exception handling (since I throw exceptions) and their response was to add catch (...) {} all over the place. Fortunatley, I log all exceptions at the source.
|
|
|
|
|
I agree.
There are some rare cases where doing nothing is the correct thing to do. A third-party library we use throws an exception if you call Init() more than once, but doesn't actually mess up the state of the library. Our app is multithreaded, so we have an empty catch block there.
Mind you, these edge cases are well documented with comments.
|
|
|
|
|
Yes, sometimes is a good reason to use an empty catch. It still only acceptable if it is well documented as to why it was done so that some poor programmer a few years down the road doesn't have to waste time trying to figure out how the exception should really be handled.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
LMAO
|
|
|
|
|
I saw this at work. I guess the coder wanted to do a series of validations and return an error when the first failure condition was found. I don't know if you'll consider this a horror or not, but it is definitely a backwards way of doing this.
Select Case (true)
Case [failure condition 1]
Throw New Exception("Condition 1 failed.")
Case [failure condition 2]
Throw New Exception("Condition 2 failed.")
'etc...
Else
'Success!
End Select
|
|
|
|
|
Looks ok for me. Why a backwards way?
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Why? Normally the test expression is evaluated at runtime, and the result is compared to each case until a match is found. In my experience, the case expressions are usually constant, and they are usually unique values (in C# & C++ these behaviors are enforced).
Here, the test expression is the constant, and the case expressions are calculated at runtime. And, since in the actual example there were several expressions that were returning boolean results, there were several cases that would have the same values.
That is what I mean by backward. It certainly works, and maybe it just struck me as odd because my background is predominantly C++ & C#. But, the company I'm at has a large body of VB.NET code and this is the only instance I've seen of a case statement done this way. And, it is backward compared to the example in MSDN as well.
It seems like if you want to evaluate a series of expressions until you find one that is true, using an if-then-elseif would be more appropriate. Just my opinion.
|
|
|
|
|
Of course it wouldn't make sense in neither C++ nor C# code. But since VB doesn't require case expressions to be constant, the syntax above is acceptable. Anyway it's not just your opinion -- I find this odd and confusing too. On the other side, I am not a VB guy and maybe it's normal to them and you have to get used to Switch (True) mess.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Perhaps more interesting would be a use I've seen suggested for "Catch ... When" in vb.net: have the "When" clause invoke a function which logs the exception and always returns true (if the goal is to catch the exception, but use a centralized logging facility) or always returns false (if the goal is to log the exception and related information without having to catch and rethrow the exception).
|
|
|
|
|
Think of it as If ElseIf statements with uniform indentation of the conditionals.
Of course in this case it could be reduced to:
If [failure condition 1] Then _
Throw New Exception("Condition 1 failed.")
If [failure condition 2] Then _
Throw New Exception("Condition 2 failed.")
'etc...
'Success!
But in defence of Select Case True , it used to be the easiest way to get short-circuiting in VB6. Could this code have been around long enough to have been converted?
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)
|
|
|
|
|
So, I'm doing a C++ class.
As I taught myself a few things in C++ and am a prper C# programmer, the first of three months is nothing but child's play.
But the problem is the teacher.
Whenever I try to do something like it's donw right, using good practices, he tell me it's not right in his course and after a short discussion where I try to tell him how it's done properly, he always says "but not in my class".
Every time I ask him something a tad more advance then what he's currently teaching, he complains about me using such advanced techniques and makes me use some beginner's stuff that is plain wrong.
The best is that he always complains and argues about my data encapsulation.
It started when he told me get and set methods were bad and I should always make every meber public, except the rare case that I have actual checking done in these methods.
And now, I created a program for the current project with a 3 tier architecture, data, console in/output and file in/output.
Yet he tells me I shouldn't and it's wrong and made me dump all methods into the data classes.
I tried to explain him that this is the right way, but as always he only tells me what he teaches is right, of course he doesn't have experience working as programmer at all but always only taught it.
I abide, because it takes only a handful minutes for me and I don't want to risk getting a lower grade because I didn't do what he said.
Did I mention that he doesn't even try to understand when don't do things 100% exactly as he says?
He just says "sorry, I don't understand" and leaves, without even looking once at the code.
But, the good part is that it's only one more week.
Monday after next I'll have a new teacher and start with C++ windows programming, I really hope that new teacher isn't such a blockhead.
modified on Friday, March 13, 2009 10:15 AM
|
|
|
|
|
That's not a coding horror; that's a teaching horror. Show some examples of what he thinks is good code.
|
|
|
|
|
Megidolaon wrote: The best is that he always complains and argues about my data encapsulation.
It started when he told me get and set methods were bad and I should always make every meber public, except the rare case that I have actual checking done in these methods. Sigh
And now, I created a program for the current project with a 3 tier architecture, data, console in/output and file in/output.
Yet he tells me I shouldn't and it's wrong and made me dump all methods into the data classes.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
The code itself is okay, but he disallows all good practices I try to use.
|
|
|
|
|
I had the same thing on my university. However I just did things 100% exactly as they wanted. Senior teachers are not a material which can be reformed, so just botch all these homeworks up, pass a damn exam and go for a beer.
And yes, I was supposed to put "Write" and "Save" methods in data containers...
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Yeah, I do as he says, no use in fighting back.
That's why I made this topic, to vent my anger...
|
|
|
|
|
He's actually a very good teacher.
The lesson here is to prepare you for dealing with your future pointy haired boss[^] that will be telling you to do far more incomprehensible things on a daily basis.
|
|
|
|