繁体   English   中英

DataTable索引超出范围异常处理,仍然引发错误

[英]DataTable Index Out of Range Exception Handled, Still Error Thrown

运行时出现奇怪的错误。 我说奇怪是因为我已经处理了可能引发错误的异常(至少在我看来)。 请阐明此主题。我试图找到答案,但找不到此特定问题的答案。 将其标记为重复的任何人都必须阅读代码。 下面是我得到的错误的快照 错误

我查看了我的代码,并特别使用DataTable.Rows.Count (对于supportPointSelected)确保不会发生这种情况。 我检查了Count,如果且仅当其大于0(代码的第一个IF语句)时,我继续进行操作。 请在下面找到代码

private List<byte> routeHandler(DataTable supportPointSelected, double taskState, int indices)
{
    //TPCANStatus statusCan = new TPCANStatus();
    int a = supportPointSelected.Columns.IndexOf("Number");     // column number indices
    int b = supportPointSelected.Columns.IndexOf("XSupport"); // column number X
    int c = supportPointSelected.Columns.IndexOf("YSupport"); // column number Y
    int d = supportPointSelected.Columns.IndexOf("VSupport"); // column number Velocity
    int v11, v22;
    byte i1, i2,
        x1, x2, x3, x4,
        y1, y2, y3, y4,
        v1, v2;
    if (supportPointSelected.Rows.Count > 0) // Check to Avoid the Error
    {
        if (Convert.ToDouble(supportPointSelected.Rows[0][d]) == 0)
        {
            supportPointSelected.Rows[0][d] = 0.01; // This is the Point the Error Occurs
        }
        else { }
        if ((taskState == 1 ) || (taskState ==2))
        {
            if (indices > 0)
            {
                // Check for Index overflow. 
                if (indices > supportPointSelected.Rows.Count)
                    indices = supportPointSelected.Rows.Count;
                else
                { }
                // Port Index into Bytes
                i1 = Convert.ToByte(Convert.ToInt16(supportPointSelected.Rows[indices - 1][a]) & 0x00FF);
                i2 = Convert.ToByte((Convert.ToInt16(supportPointSelected.Rows[indices - 1][a]) & 0xFF00) >> 8);
                // Port X into Bytes
                x1 = Convert.ToByte(Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0x00FF);
                x2 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0xFF00) >> 8);
                x3 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0xFF0000) >> 16);
                x4 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][b]) * 100) & 0xFF000000) >> 24);
                // Port Y into Bytes
                y1 = Convert.ToByte(Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0x00FF);
                y2 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0xFF00) >> 8);
                y3 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0xFF0000) >> 16);
                y4 = Convert.ToByte((Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][c]) * 100) & 0xFF000000) >> 24);
                // Port Velocity into Bytes
                v11 = Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][d]) * 100);
                v1 = Convert.ToByte(v11 & 0x00FF);
                v22 = (Convert.ToInt32(Convert.ToDouble(supportPointSelected.Rows[indices - 1][d]) * 100) & 0xFF00);
                v2 = Convert.ToByte((v22 & 0xFF00) >> 8);
            }
            else
            {
                i1 = 1;
                i2 = 0;
                x1 = 0;
                x2 = 0;
                x3 = 0;
                x4 = 0;
                y1 = 0;
                y2 = 0;
                y3 = 0;
                y4 = 0;
                v1 = 0;
                v2 = 0;
            }
        }

        else
        {
            // Porting Index into Bytes
            i1 = 0;
            i2 = 0;
            // Porting X into Bytes
            x1 = 0;
            x2 = 0;
            x3 = 0;
            x4 = 0;
            // Porting Y into Bytes
            y1 = 0;
            y2 = 0;
            y3 = 0;
            y4 = 0;
            // Porting Velocity into Bytes
            v1 = 0;
            v2 = 0;
        }
    }
    else {
        i1 = 0;
        i2 = 0;
        x1 = 0; x2 = 0; x3 = 0; x4 = 0;
        y1 = 0; y2 = 0; y3 = 0; y4 = 0;
        v1 = 0; v2 = 0;                 
    }
    List<byte> output = new List<byte>();
    output.Add(i1);
    output.Add(i2);
    output.Add(x1);
    output.Add(x2);
    output.Add(x3);
    output.Add(x4);
    output.Add(y1);
    output.Add(y2);
    output.Add(y3);
    output.Add(y4);
    output.Add(v1);
    output.Add(v2);
    return output;
}

在执行方法时,supportPointSelected是否固定?或者是否有其他线程正在更改它? 发生异常时,哪个值不正确? d不正确还是没有第0行? 您可以修复列索引并尝试以这种方式对其进行调试吗? 我知道这不是一个真正的答案,但缺少太多内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM