如何正确使用:System.InvalidOperationException

时间:2014-02-05 21:17:01

标签: c# exception-handling unity3d

这是Unity C#

//...
    public static void Interupt(int Index, string Text){
        try{
            Change(Transforms[ Index ], Text);
        }
        catch{
            throw new System.InvalidOperationException("Index: " + Index + " Is too large should be less than: " + Transforms.Count); // points me here
        }
    }
}

好的,这段代码指向我

throw, ...

如何让它指向调用函数的行?

像:

using UnityEngine;
using System.Collections;

public class TestScript : MonoBehaviour {
    void Start(){
        SomeClass.Interupt(5, ""); // I want it to point me here
    }
}

我确实尝试过:

return throw new System.InvalidOperationException("Index: " + Index + "  Is too large should be less than: " + Transforms.Count);

但我明白了:

error CS1525: Unexpected symbol `throw'
女巫是完全合乎逻辑的。

但是我无法弄清楚Unity是如何处理它指向函数而不是抛出线条的东西?

我希望你们中的任何人都了解Unity Engine

1 个答案:

答案 0 :(得分:0)

如果你只是希望调试器跳转到调用Interrupt的函数而不是直接跳转到Interrupt,你可以用DebuggerStepThroughAttribute装饰中断方法,即

    [DebuggerStepThrough]
    public static void Interupt(int Index, string Text)
    {
        try
        {
            Change(Transforms[Index], Text);
        }
        catch
        {
            throw new System.InvalidOperationException("Index: " + Index + " Is too large should be less than: " + Transforms.Count); // points me here
        }
    }

如果要以编程方式进行分析,则必须使用调用堆栈跟踪。

相关问题