0

Unity3D version: 5.6.1f1 Personal.

Summary / TLDR

How do I get the Unity3D debugger to show an exception in the calling function/method?


Details

I'm trying to use a static class/method to make null checks easier. In this static class Ensure I have a static method NotNull with the System.Diagnostics.DebuggerStepThrough attribute on it. This method does the typical null check and exception throwing for me. This attribute is supposed to, as I understand it, make it look like the calling method is throwing the exception. It's not.

The unity debugger still shows the exception being thrown in the static class's method. Does anyone know what I'm missing to get the debugger to work as I described earlier? I've tried this with JMC enabled and disabled.

Static class & method

public static class Ensure
{
    [System.Diagnostics.DebuggerStepThrough]
    public static void NotNull(object valueToTest, string valueName)
    {
        if (valueToTest == null)
            throw new System.NullReferenceException(valueName + " is null");
    }
}

Calling the method

public class foo
{
    public Timer()
    {
        object obj = null;
        
        Ensure.NotNull(obj, "obj");
    }
}
Community
  • 1
  • 1
CheeseMo
  • 167
  • 2
  • 3
  • 12

1 Answers1

0

You're confusing us.

The DebuggerStepThrough Attribute instructs the debugger to step through the code instead of stepping into the code. Its typically used in methods like InitializeComponent() because rarely will you want to step through Design-Time generated code.

I think what you meant to write is the attribute throwing the exception makes it look like the caller method is throwing the exception. That is not right.

Here is the famous QA that discusses it, basically throwing the exception rather than just a throw loses the stacktrace.

Here's a pro tip: Exceptions are supposed to be used exceptionally. They are expensive. Currently you are using Exceptions when you dont need to. The NotNull void would be better as a boolean function:

public static bool IsNotNull(object valueToTest)
{
    return (valueToTest != null);
}

This way if it returns false, you can throw the exception in the caller. That's how you can achieve - what I think it is - you're trying to do.

A better approach altogether is to use the ? null-conditional operator introduced in C# 6.0

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • As far as I can tell, unity3d uses C# ~4 so I sadly can’t use the null-conditional. Also, I should’ve mentioned that I’m trying to conform to the Guard-Clause pattern (http://deviq.com/guard-clause/). Stepping through/over the method throwing the exception in this pattern makes following the stack trace and debugging easier. I’ll make edit that into my post as soon as I can. – CheeseMo Oct 04 '17 at 02:32
  • [Unity 2017.1 supports C# 6 and .Net 4.6](https://blogs.unity3d.com/2017/07/11/introducing-unity-2017/). For Guard Clauses try not to go too far overboard http://blog.ploeh.dk/2013/07/08/defensive-coding/ – Jeremy Thompson Oct 04 '17 at 02:50