Why, while do-while looping, does an exception thrown in do-scope not halt while-condition-scope execution?

Rogers 0 Reputation points
2024-10-12T03:41:58.6033333+00:00
public static int? DecimalScale() {
        dynamic? result = null;
        int maxParameterLength = 0;
        ConstructorInfo[] constructors = typeof(decimal).GetConstructors();
        foreach (ConstructorInfo constructor in constructors) {
            ParameterInfo[] parameters = constructor.GetParameters();
            int parameterLength = parameters.Length;
            if (parameterLength != maxParameterLength && parameterLength == (maxParameterLength = maxParameterLength < parameterLength ? parameterLength : maxParameterLength)) {
                Type type = parameters.Last().ParameterType;
                FieldInfo? fieldInfo = type.GetField("MinValue", BindingFlags.Public | BindingFlags.Static);
                if(fieldInfo != null) {
                    result = fieldInfo.GetValue(type);
                }
            }
        }
        bool halt = false;
        try {
            do {
                   new decimal(1, 0, 0, false, result);
            } while(0 < ++result);
        } catch (ArgumentOutOfRangeException) {
            result--;
        }
        return result;
    }

Now from my testing;

Internal try-catch does not alter it, utilization of surrounding boolean flags does not alter it, directions of terms do not alter it;

What that means to me is - when a do-while loop is looping the increment operation/operator must take priority even over the conditions of the while-scope/do-while loop to the extent of the scope of the do-while loop being skipped for incrementation...

If inside my loop, an exception is fired - I cannot prevent an incrementation operation occuring in my while loops conditioning whit any pattern I have found.

What are we pulling out a list from within the while condition of each term, handling incrementation then checking the condition state? Well yes -

But how could a condition state of my do-while loop find priority over attempting to exit the loop?

Is it threading? Or...is the interrupt signal misaligned?

Why fire an incrementation, post halt for exception, to check a condition - which had passed prior to such throw?

Anyways...also could we get a Scale range for Decimals?

Developer technologies .NET Other
Developer technologies Visual Studio Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Muhammad Zia Ul Haq 0 Reputation points
    2024-10-12T18:47:32.7566667+00:00

    Your observations about the behavior of the do-while loop and the incrementation inside it are valid and highlight an important aspect of how loops and exception handling work in C#. Let’s break down your concerns and then address your request regarding the scale range for decimals.

    Behavior of the do-while Loop

    Loop Execution Flow: In a do-while loop, the body of the loop is executed at least once before the condition is checked. This means that any code inside the loop, including incrementation (++result), will execute before the condition is evaluated. If an exception is thrown during the execution of the loop body, the flow will jump to the nearest catch block, and the condition will be checked after that.

    1. Incrementing the Result: In your loop:

    ** do { new decimal(1, 0, 0, false, result); } while(0 < ++result);

    **

    The expression ++result is evaluated after the body of the loop executes, regardless of whether an exception occurs or not. If an exception is thrown, the incrementation will still happen before the next condition check.

    Exception Handling: When an exception occurs, control jumps to the catch block, and in your case, you decrement result to compensate for the last incrementation. However, this might lead to an unexpected behavior where result could potentially be incremented even after an exception was raised.

    Threading and Interrupt Signals: This behavior is not related to threading or misaligned interrupt signals. Instead, it's about the control flow in C#. The incrementation is part of the loop body and happens regardless of the state after handling an exception.

    Finding the Scale Range for Decimals

    In C#, the decimal type has a fixed scale of up to 28-29 significant digits. The scale refers to the number of digits to the right of the decimal point. Here's how you can get the scale range for decimal values programmatically:

    **public static (int minScale, int maxScale) GetDecimalScaleRange() { // The minimum scale for decimal is always 0. int minScale = 0;

    // The maximum scale for a decimal is defined as 28.
    int maxScale = 28;
    
    return (minScale, maxScale);
    

    }**

    
    To summarize:
    
    - The behavior of your `do-while` loop is standard in C# and reflects how exception handling interacts with loop execution.
    
    - The scale range for decimal values is typically from `0` to `28`, indicating the number of decimal places that can be represented.
    
    If you want more control over how your loop increments or handles exceptions, you might consider restructuring the loop or managing the state of `result` in a different way to avoid unintended increments after exceptions.
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.