Possible Duplicate:
Defensive Programming vs Exception Handling?
I don't know, that this question fit better on this site, or Stack Overflow, but because my question is connected rather to practices, that some specified problem.
So, consider an object that does something. And this something can (but should not!) can go wrong. So, this situation can be resolved in two way:
first, with exceptions:
DoSomethingClass exampleObject = new DoSomethingClass();
try
{
exampleObject.DoSomething();
}
catch (ThisCanGoWrongException ex)
{
[...]
}
And second, with if statement:
DoSomethingClass exampleObject = new DoSomethingClass();
if(!exampleObject.DoSomething())
{
[...]
}
Second case in more sophisticated way:
DoSomethingClass exampleObject = new DoSomethingClass();
ErrorHandler error = exampleObject.DoSomething();
if (error.HasError)
{
if(error.ErrorType == ErrorType.DivideByPotato)
{
[...]
}
}
which way is better? On one hand, I heard that exceptions should be used only for real unexpected situations, and if programmer knows that something may happen, they should use if/else. On the other hand, Robert C. Martin in his book Clean Code wrote that exceptions are far more object oriented, and more simple to keep clean.

