Search code examples
c#exceptionunhandled-exception

Exception not thrown through the function but directly caught


I have a function Overwrite which throw an exception when an object is locked by the user.

For now, my code is

 public class MyObject<T>
{
    //...other methods,...
    public void Overwrite(T value)
    {
        if (IsLocked)
            throw new Exception("You cannot touch the data you don't own");
        this.Value = value;
        OnUpdate?.Invoke(value);
    }
}

I use several MyObject in my code and when the IsLocked is set to true, my exception is directly captured by the IDE in the Overwrite method. So, I cannot see where the error comes from (I mean, the line where I can find the faulty call to Overwrite.

See below :enter image description here

Where I want my exception is when I call MyObject.Overwrite(), then I can see the faulty line (and the faulty object).

I've tried several techniques like try-catch-throw,... but none work.

By the way, it's the first time I'm creating an Exception in CSharp and usually all the exceptions I caught in the past were unhandled at the faulty line...

EDIT : I expect more something like this : enter image description here


Solution

  • Try to use Call Stack window. You can open it via Debug > Windows > Call Stack

    call stack