c# 2004 (Whidbey)

  • Thread starter Arman Oganesian
  • Start date
A

Alvin Bruney

catch during testing, and forget to remove it. And yet another reason is
that developers sometimes don't realise that you shouldn't catch
System.Exception without rethrowing it. Finally, a few of the .NET base

i totally disagree with that statement. i'm here wondering why you made it.
would you care to explain? I can partially see your point but that statement
is just too general to let slide.
 
M

Mark Pearce

Hi Alvin,

The current "best practice" for exception management is that you shouldn't
catch an exception unless you expected that exception, you understand it,
and you're going to deal with it. Instead, you should let exceptions that
you don't know how to handle bubble upwards to code that does know how to
handle that exception, or until the top-level exception handler is reached.

Catching System.Exception (without re-throwing it) is bad because it's
stating that you know how to handle *every* type of CLS-compliant exception,
even unusual exceptions such as ExecutionEngineException,
OutOfMemoryException and StackOverflowException. In general, your code won't
know how to handle *every* type of exception.

Of course, sometimes you need to catch every exception, such as when you
need to reverse a transaction or you want to create and throw a more
meaningful custom exception. But in each one of these cases, you're
re-throwing the System.Exception in some form.

Regards,

Mark
--
Author of "Comprehensive VB .NET Debugging"
http://www.apress.com/book/bookDisplay.html?bID=128


Alvin Bruney said:
catch during testing, and forget to remove it. And yet another reason is
that developers sometimes don't realise that you shouldn't catch
System.Exception without rethrowing it. Finally, a few of the .NET base

i totally disagree with that statement. i'm here wondering why you made it.
would you care to explain? I can partially see your point but that statement
is just too general to let slide.
 
N

Niall

Mark Pearce said:
Hi Niall,
So somebody, somewhere, *always* cares about the code's internal quality.

Of course, but my point was that code that has a nice design, but doesn't
work is much harder to sell to the customer than code that could have its
design improved, but it actually does the job. I'm not saying that I have a
"just get it done, we can make it pretty later" attitude, just that it's
more important to make sure the program does what is needed. From my
experience, once you get a decent sized system, you can be forever
"improving" the design, because the design of the architecture can never
really fully facilitate all cases of usage.

The developer in question thought that he *was* targeting a specific area of
code. He didn't know about the optimisation, and indeed had no way of
knowing about the optimisation without stepping through the code in a
source-level debugger.

Well, presumably the designer was aware of all the code in the method they
had written? The test should directly cause the methods being tested to be
run. To me, this is what testing in isolation means.

Of course in hindsight, it should have been coded better. But you've just
been arguing that the internal code quality doesn't really matter, as long
as the unit tests are satisfied. You can't have it both ways.

As had been said before, bad practice in coding, unit testing or step
through debugging can bring both sides down. If the coder had stepped
through the function without causing the other type of exception to be
thrown, then no one would know about it. All I'm saying is that better
coding of the original method would have allowed the unit test to pick up
the fault. Unit testing doesn't ensure perfect coding practices, and neither
does stepping through with a debugger...

planning to wrap the exception in a more meaningful one and re-throw. <<

There are various reasons to catch System.Exception, including the one you
mentioned. Another reason is to reverse a transaction after any exception.
Yet another reason is that many developers often put in a System.Exception
catch during testing, and forget to remove it. And yet another reason is
that developers sometimes don't realise that you shouldn't catch
System.Exception without rethrowing it. Finally, a few of the .NET base
class methods suppress all exceptions for "security" reasons, and just fail
silently.

Whatever the reason, you won't find this type of requirements/coding bug
unless you step through the code and find that an unexpected exception type
was being silently thrown and caught.

Any exception that escapes the function will cause the unit test to fail, so
if the coder was wrapping the exception and rethrowing, the unit test would
have caught it. If it was rolling back the transaction and then rethrowing,
the unit test would have caught that too. The only case that breaks the unit
test is when the exception is swallowed, which is bad practice. You can use
code smell type software to pick out this kind of thing. If you rely only on
the step through, you rely on the problem situation raising its head during
that one case of the step through.

Niall
 
J

Jon Skeet

Alvin Bruney said:
Nial has made such a big big deal about this unit testing thing that i am
willing to give it a second look. if you provide me a link (i think i have
one by the way, not sure) i am willing to give it an honest to God shot in
my current project. Note that every line of code i wrote has been stepped
thru so i guess i can benefit from the unit test right? what have i got to
lose? I'll give an honest analysis after i'm done.

http://nunit.sf.net for NUnit itself

and there are lots of articles about unit testing linked off
http://www.junit.org

Good luck with it - I'll look forward to your analysis.
 
J

Jon Skeet

Pete said:
Is it me or are all the unit testing utilities identical.

I think I'll use csUnit.. just because I like their website better :)

I suspect they're likely to be 90% the same - but different people will
each need the different 10% that different projects supply :)
 
N

Niall

Yeah, I think they're probably much the same. I would expect NUnit and JUnit
to be similar...

Pete, if I were you, I'd have a look at the features of a few of them and
make your decision. If you go ahead with the testing and end up with several
thousand unit tests, it may be a pain to change to another testing platform
if you need to :p

I would definitely recommend that whatever test platform you use, you get
one that's open source. If you use testing a lot, it becomes part of the
culture, and you can come up with a lot of features which are helpful for
the team which aren't in the original product. You can also bend the
behaviour of the existing code to suit your purposes better.

I'll be interested to read Alvin's experiences, I hope they're positive.

Niall
 
J

Jon Skeet

Niall said:
Yeah, I think they're probably much the same. I would expect NUnit and JUnit
to be similar...

The were much *more* similar earlier on - NUnit has now been rewritten
to use attributes rather than relying on naming conventions, which
strikes me as a thoroughly good thing.

I would definitely recommend that whatever test platform you use, you get
one that's open source.

You mean there are some which aren't open? :)
 

Ask a Question

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.

Ask a Question

Top