简体   繁体   English

如何正确使用:System.InvalidOperationException

[英]how to properly use: System.InvalidOperationException

This is Unity C# 这是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
        }
    }
}

ok this code points me to 好的,这段代码将我指向

throw, ...

how do I make it to point me to line where the function was called? 如何使我指向调用该函数的行?

like: 喜欢:

using UnityEngine;
using System.Collections;

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

THO I did try doing: 我确实尝试过这样做:

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

but I get: 但我得到:

error CS1525: Unexpected symbol `throw'

witch is totally logical. 女巫是完全合乎逻辑的。

BUT what I can't figure out is how does Unity handle this things that it points us to functions and not throw lines? 但是我不知道的是Unity如何处理将我们指向函数而不是抛出线的事情?

I hope any of you has knowledge in Unity Engine 希望大家对Unity Engine有所了解

If you just want the debugger to jump to the function that called Interrupt instead of jumping to Interrupt directly, you could just decorate the Interrupt method with the DebuggerStepThroughAttribute , ie 如果只希望调试器跳转到调用Interrupt的函数,而不是直接跳转到Interrupt,则可以使用DebuggerStepThroughAttribute装饰Interrupt方法,即

    [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
        }
    }

If you want to analyze this programmatically, you have to use the call stack trace. 如果要以编程方式进行分析,则必须使用调用堆栈跟踪。

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

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