My question boils down to : “Why not use exception (or error- for that matter) handling for regular program flow? To avoid all standard-answers I could have googled on, I will provide an example you all can attack at will.
C# and Java (and too many others) have with plenty of types some of ‘overflow’ behaviour I don’t like at all.
(type.MaxValue + type.SmallestValue == type.MinValue
for example : int.MaxValue + 1 == int.MinValue
).
But, seen my vicious nature, I’ll add some insult to this injury and even expand this behaviour to, let’s say an
Overridden DateTime type. (I know DateTime is sealed in .NET, but for the sake of this example, I’m using a pseudo language
that is exactly like C#, except for the fact that DateTime isn’t sealed :-))
The overridden Add method :
/// <summary>
/// Increments this date with a timespan, but loops when
/// the maximum value for datetime is exceeded.
/// </summary>
/// <param name="ts">The timespan to (try to) add</param>
/// <returns>The Date, incremented with the given timespan.
/// If DateTime.MaxValue is exceeded, the sum wil 'overflow' and
/// continue from DateTime.MinValue.
/// </returns>
public DateTime override Add(TimeSpan ts)
{
try
{
return base.Add(ts);
}
catch (ArgumentOutOfRangeException nb)
{
// calculate how much the MaxValue is exceeded
// regular program flow
TimeSpan saldo = ts - (base.MaxValue - this);
return DateTime.MinValue.Add(saldo)
}
catch(Exception anyOther)
{
// 'real' exception handling.
}
}
Of course an if could solve this just as easy, but the fact remains that I just fail to see why you couldn’t use exceptions (logically that is, I can see that when performance is an issue that in certain cases exceptions should be avoided). I think in many cases they are more clear than if-structures and don’t break any contract the method is making.
IMHO The “Never use them for regular program flow”-reaction everybody seems to have is not that well underbuild as the strength of that reaction can justify.
Or am I mistaken?
I've read other posts, dealing with all kind of special cases, but my point is : if you are 1. Clear and 2. Hounour the contract of your method,
there's nothing wrong with it. Shoot me.