简体   繁体   中英

how to properly use: System.InvalidOperationException

This is 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?

I hope any of you has knowledge in 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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