3

I actually think that I got it but need some additional confirmation from you because I want to understand it and do it the correct way! I still haven't found a straight line in abstracting exception handling and logical code. So I try to get closer to it with your help.

Let's consider I call the function "foo(0)":

// Code 1
int foo(int a)
{
    int value = 10;
    return value/a;
}

This will throw "division by zero" exception.

But if parameter "a" must be inside of a specific range for function "bar" (Code 2) to be able to return a valid result (e.g. in the range of [5...10]), calling it with a value outside this range (e.g. 3) would of course not throw an exception unless I define one. So for this situation I define an exception, don't I?

For example this way:

// Code 2
void bar(int b)
{
    if (b < 5)
    {
        throw new ArgumentException("Your input parameter is below minimum acceptable value");
    }
    else if (b > 10)
    {
        throw new ArgumentException("Your input parameter is above maximum acceptable value");
    }
    else
    {
        output(b);
    }
}

or shouldn't exceptions be used for that (well I think they're here for exactly this purpose) and I rather do it this way?

// Code 3
int bar(int b)
{
    int error = 0

    if (b < 5)
    {
        error = -1;
    }
    else if (b > 10)
    {
        error = -2;
    }
    else
    {
        output(b);
    }

    return error;
}

Thx for your input(s).

Cheers

(The reason why I ask is because I do have a source code of a released software in front of me that has both. I have not much experience in OOP, consequently not either with "try-catch". And, believing the post found and the youtube tutorials I went through, this topic is very misunderstood. And I guess the developer of the software in front of me did misunderstand it. Tell me if I am wrong.)

4 Answers 4

2

Why not error codes (int bar(int b) {...}) but exceptions? Two main reasons are

  1. You can easily and unintentially ingore the error code returned; debugging can well appear to be painful. In case of exception thrown, you'll known it imediately (routine's crash)
  2. Error code like 3, -14 etc. provides very little info on what's going on; exceptions can tell a lot about the cause: at least exception type (what is the problem; here we have ArgumentOutOfRangeException - method's argument out of range), message (problem's description) and stack trace (where is the problem)

I suggest a bit different code:

void bar(int b)
{
    // Validation

    if (b < 5)
        //DONE: not just ArgumentException, but ArgumentOutOfRangeException
        //DONE: nameof(b) - which argument has wrong value (useful if you have several arguments) 
        //DONE: when complaining for below/above I suggest developer to know the boundaries 
        throw new ArgumentOutOfRangeException(
            nameof(b), 
          $"Your input parameter b = {b} is below minimum acceptable value {5}");
    else if (b > 10)
        throw new ArgumentOutOfRangeException(
            nameof(b), 
          $"Your input parameter b = {b} is above maximum acceptable value {10}");

    // Argument(s) is / are valid, main routine here

    output(b);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Dmitry for your code suggestion and valuable notes that you added!
2

The idiomatic way to report an error in C# is to through an exception. Returning error codes could, of course, technically work, but it's just not the way things are done in C#.

1 Comment

ErrorCodes does also brings so called Documentation Nightmare
1

Errors should be returned by throwing an error with an appropriate type (imo). This allows the caller to be notified and handle the exception, explicitly.

Code in example "code 3" worries me. It couples the calling code to this method to know what to do based on the return value. There is no indication that the return is an error and that it will manipulate the input value. Whenever possible be explicit.

Comments

0

You can use Checked Exception feature in C# to handle them in caller methods by their types. The following Nuget package adds this feature into the C# language. Portia.Roslyn.CheckedException

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.