显示来自调用者的argumentoutofrange

时间:2013-02-24 02:20:18

标签: c# debugging

我想知道是否有办法从方法/函数的调用者那里获得“ArgumentOutOfRangeException”:

几天来一直在寻找这个,没有运气...... 搜索参数的罕见或错误选择...

示例:

class Main
{
    struct XYZ
    {
        public int X, Y, Z;
        public XYZ(int x, int y, int z)
        {
            X = x;
            Y = y;
            Z = z;
        }
    }
    class ArrayE<T>
    {
        public T[, ,] data;

        public ArrayE(XYZ xyz)
        {
            data = new T[xyz.X, xyz.Y, xyz.Z];
        }
        public T this[XYZ xyz]
        {
            get
            {
                if (OutOfBounds(xyz))
                {
                    throw new ArgumentOutOfRangeException(); // Error shows here in debug
                }
                else
                {
                    return data[xyz.X, xyz.Y, xyz.Z];
                }
            }
            set
            {
                if (OutOfBounds(xyz))
                {
                    throw new ArgumentOutOfRangeException(); // Error shows here in debug
                }
                else
                { 
                    data[xyz.X, xyz.Y, xyz.Z] = value; 
                }
            }
        }

        bool OutOfBounds(XYZ xyz)
        {
            return xyz.X < 0 | xyz.Y < 0 | xyz.Z < 0 |
                       xyz.X >= data.GetLength(0) |
                       xyz.Y >= data.GetLength(1) |
                       xyz.Z >= data.GetLength(2);
        }
    }

    ArrayE<int> Data;
    public void Test()
    {
        XYZ xyz = new XYZ(10, 10, 10);
        Data = new ArrayE<int>(xyz);

        xyz = new XYZ(1,0,2);
        Data[xyz] = 2;


        xyz = new XYZ(1, -1, 2);
        Data[xyz] = 4; // I would like the debugger to stop here
        //As if I did this:
        Data[xyz.X,xyz.Y,xyz.Z] = 4 // The debugger would then stop at this line

        //Example of what I want
        int[] array = new int[10];
        array[2] = 1;
        array[-1] = 3; // This is the behavior I would like to happen when out of bounds, The error shows here.
    }
}

修改:更具体:
当我启动debuggin时,我希望调试器停在“Data [xyz] = 4;”行。其中xyz是一个无效索引,所以我可以看到它从哪里调用,而不是从Data [xyz] get set函数内部。

如果常规int []将获得无效索引,例如-1,则调试器将停止 在那一行,你可以在访问数组之前查看这些行,这样就可以找出索引的原因。 使用当前设置,当在ArrayE [xyz]上使用无效的xyz索引时,调试器在get集内停止,使我无法跟踪并找出xyz超出数组边界的原因... < / p>

希望关于我所询问的内容更清楚 我可以使用try catch,但没有别的方法吗?执行try catch会很快使代码难以理解且无法管理,因为程序非常大......

那么如果可能的话怎么办呢?

我正在使用Visual C#2010 Express

1 个答案:

答案 0 :(得分:0)

刚刚运行此代码,您发布它似乎正在做您想要的。下面是调用者捕获和处理它的一个例子。如果你想在测试函数中处理这些,你只需要移动try catch。

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            Main m = new Main();
            m.Test();
        }
        catch (Exception e)
        {
            Console.Write("Uh oh :O " + e.StackTrace);
        }
    }
}

编辑: 好的,看了这之后,我想出了一个方法去做你要问的事。您必须设置DebugerHidden属性。例如:

        public T this[XYZ xyz]
        {
            //add attribute here to say this function is 'ignored'
            [DebuggerHidden]
            get
            {
                if (OutOfBounds(xyz))
                {
                    throw new ArgumentOutOfRangeException(); // Error shows here in debug
                }
                else
                {
                    return data[xyz.X, xyz.Y, xyz.Z];
                }
            }
            //add attribute here to say this function is 'ignored'
            [DebuggerHidden]
            set
            {
                if (OutOfBounds(xyz))
                {
                    throw new ArgumentOutOfRangeException(); // Error shows here in debug
                }
                else
                {
                    data[xyz.X, xyz.Y, xyz.Z] = value;
                }
            }
        }