如何检查文件的IndexOutOfRangeException并将其记录?

时间:2019-10-25 14:24:24

标签: c# unity3d debugging error-handling

更新

try
        {
            //Attemps string conversion for each of the point's variables
            int.TryParse(row[0], out q.pointID); //Checks for existence of data on the line...
            float.TryParse(row[1], out q.xValue); //Input x-value
            float.TryParse(row[2], out q.yValue); //Input y-value
            float.TryParse(row[3], out q.zValue); //Input z-value
            float.TryParse(row[4], out q.tempValue); //Input temp-value
        }
        catch (IndexOutOfRangeException)
        {
            Debug.Log("File out of range...");
            errorLogScript.errorCode = 1100;
            SceneManager.LoadScene(4);
        }

这是我当前拥有的代码,但是每当我尝试将场景转移到errorScreen时,它似乎都冻结了。话虽如此,我没有收到错误,但是我的代码死机了,每当我尝试测试此错误时,Unity都会崩溃。

有人对我该如何解决有任何想法吗?

OP

我目前正在Unity中开发一个应用程序,我想创建一个错误/崩溃报告系统,该系统在加载失败时向用户显示唯一的错误代码。由于这个特定的应用程序将被许多具有许多不同技能的人使用,因此我想在今年晚些时候发布之前,尽可能地打破它。为此,我想快速参考一下,以便用户可以在文档中查找。

以下代码演示了如果用户输入的文件路径不存在会发生什么情况...

if (File.Exists(dropDownMenuScript.dataFileName + ".csv"))
    {
        Debug.Log("File found, loading..."); //Debugs success to console
    }
    else
    {
        Debug.Log("File not found, aborting..."); //Debugs the problem to console
        errorLogScript.errorCode = 1000; //Shows the code "E1000"
        SceneManager.LoadScene(4); //Loads the error-screen which displays the code
    }

我最近发现了另一个读取的错误; “ IndexOutOfRangeException”-在这种情况下,与文件的解析有关,这意味着它存在,但不符合与程序兼容的数据格式。我想为此问题创建另一个错误日志,但是我确实知道如何执行此操作,因为它是Unity编辑器错误。

如果这不是很清楚,我深表歉意,但是如果需要,我会提供所需的任何上下文。谢谢!

2 个答案:

答案 0 :(得分:1)

您不能使用try-catch块专门针对IndexOutOfRangeException的陷阱吗?

try
{
    //Your code...
}
catch(IndexOutOfRangeException iore)
{
    //Log here
}

答案 1 :(得分:-1)

这是我现在所拥有的,但是它给我“ catch(IndexOutOfRangeException iore)”错误。它说 iore 已初始化但从未使用过。还需要为 IndexOutOfRangeException 调用目录吗?应该注意的是,我正在使用Unity Engine。

try { //Attemps string conversion for each of the point's variables int.TryParse(row[0], out q.pointID); //Checks for existence of data on the line... float.TryParse(row[1], out q.xValue); //Input x-value float.TryParse(row[2], out q.yValue); //Input y-value float.TryParse(row[3], out q.zValue); //Input z-value float.TryParse(row[4], out q.tempValue); //Input temp-value (For use in Heatmaps) } catch (IndexOutOfRangeException iore) { Debug.Log("File out of range..."); errorLogScript.errorCode = 1100; SceneManager.LoadScene(4); }