C# Flow Control

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hi,

I've read a few contradictory statements on how to do flow control between
methods. Main problem is that I've seen recommendations (or "standards") to
*not* use exceptions for flow control, but also that exceptions now replace
the need to pass return codes between methods.

So whats best to use?

Thanks.
 
A little article is....

http://www.c-sharpcorner.com/Code/2005/March/ExceptionManagement.asp

There are many, many articles on the web dealing with exception best
practices.

Return codes on methods can occur in all kinds of scenarios. Talking to
hardware, COM interop etc. Generally, it's best to wrap such interfaces in
OO exception savvy code if they're going to be used extensively from OO
code - mainly because branching on return codes is usually indicative of a
procedural design.

If you're writing C# that branches on return codes, ask yourself whether you
would be better served by an OO solution.

Regards,

Tim Haughton
 
Thanks Tim.

So it seems that either method (return codes, throwing exceptions) may be
acceptable in certain situations in order to allow branching in the caller
method.

Unless when you are not referring to throwing exceptions when you mention an
OO solution?
 
Dave said:
Thanks Tim.

So it seems that either method (return codes, throwing exceptions) may be
acceptable in certain situations in order to allow branching in the caller
method.

Unless when you are not referring to throwing exceptions when you mention an
OO solution?

Yes that's basically what I'm saying. If you write code that forces clients
of your code to branch, you're writing procedural code, and you're forcing
them to write procedural code too. If your code demonstrates good exception
handling, then you're more likely to be writing OO code and encouraging (but
not forcing) your clients to write OO code to use it.

Regards,

Tim Haughton
 
Dave-

There is no absolute answer to this. Exceptions are a perfectly fine
mechanism for flow control between methods, but they should only be used in
truly exceptional cases. The reason for this is twofold. First,
dogmatically speaking, exceptions are intended for exceptional situations,
not for ones likely to occur. Second, pragmatically speaking, exceptions are
costly (read, not performant) so their use should be confined to situations
where they are appropriate. I wrote a brief article that talks about this in
terms of parsing types here:

http://www.codedaily.com/Post_View.aspx?postId=6

Regards-
Eric
 
Exceptions are just that...used to catch exceptions and not to report
"status" unless that status is part of an exception such as a network
communication failure - it's not expected, or a database unavailable.

I think the issue in the original post is the confusion of the term "return
codes" versus "return references".

Exception should be used to replace "return codes" where a return code
defines a type of unexpected problem (DBMS is down, network unreachable).
For other flow control you can still return something of value - its just
not called a return code necessarily :-)

JCE
 
The article you linked to is hit-and-miss. It contains some good
advice, but also contains a few bits of bad advice, and I found it
rather difficult to read.

Most of the methods you write will naturally return things. The
properties you write will definitely return things. Most of the time
these will be object references. The usual C# idiom for "information
not available" is to return null. So, in that case, you are doing some
"flow control" based on a return value:

Employee e = Employee.GetEmployee(employeeNumber);
if (e == null)
{
... handle "no such employee" case ...
}
else
{
... found the employee ...
}

A method like GetEmployee() should throw an exception only if something
awful and unexpected happens. Depending upon your design, an invalid
employee number may be commonplace or "awful and unexpected". Design
accordingly.

If you don't know ahead of time how your method is going to be used, so
an invalid employee number may be either "awful and unexpected" or
commonplace, throw an exception but provide a validation method:

if (!Employee.IsEmployeeNumberValid(employeeNumber))
{
... handle invalid employee number ...
}
else
{
Employee e = Employee.GetEmployee(employeeNumber);
...
}

One point on which the article errs is that it shows catch clauses for
the Exception type in several places:

try
{
...
}
catch (Exception ex)
{
...
}

You should (almost) never catch an Exception! (Almost) all of your
catch clauses should catch specific types of exceptions, not the
general Exception type. You shouldn't be catching most exceptions
anyway. Instead, you should let them percolate up the call stack and
catch them in global exception handlers.

I find Microsoft's writeup on exception best practices much more
readable and reliable:

http://msdn.microsoft.com/library/d...l/cpconBestPracticesForHandlingExceptions.asp
 
I think the issue in the original post is the confusion of the term
"return
codes" versus "return references".

Exception should be used to replace "return codes" where a return code
defines a type of unexpected problem (DBMS is down, network unreachable).
For other flow control you can still return something of value - its just
not called a return code necessarily :-)

Possibly. When I think of return codes, I think of a method returning an
int, then a dirty great switch statement to decide what to do.

Tim Haughton
 

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

Back
Top