How to make C# application crash

后端 未结 12 2074
渐次进展
渐次进展 2020-12-31 13:00

I want to test if my application crash dump can be debugged. But firstly, I need to generate a crash dump of my application. I\'m using C# to code my app, and have tried wit

相关标签:
12条回答
  • 2020-12-31 13:37

    The following will provide an unhandled exception and will ask for you to choose a debugger:

    System.Diagnostics.Debugger.Launch()
    
    0 讨论(0)
  • 2020-12-31 13:42
     int[] x = {0};
     int blah = x[2];
    

    will cause an exception just as well

    0 讨论(0)
  • 2020-12-31 13:43

    It's easy enough to reproduce if you try to transform a null game object. For example, like this:

    public static GameObject gameObjectCrash;
    public void GenerateCrash()
    {
        gameObjectCrash.transform.rotation = Quaternion.Euler(90, 0, 0);
    }
    
    0 讨论(0)
  • 2020-12-31 13:43

    Use below code to close the application.

    Environment.Exit(1);    
    

    Exit needs a parameter called exitcode. If exitcode=0 means there was no error. Supply a non-zero exit code to to reflect an error.

    0 讨论(0)
  • 2020-12-31 13:45

    Throw an exception :)

    throw new Exception("Your exception here!");
    
    0 讨论(0)
  • 2020-12-31 13:45

    Well. The only good 100% way actualy crash CLR is to inject a native exception into the managed world.

    Calling the Kernel32.dll's RaiseException() directly will immediately crash ANY C# application, and Unity Editor as well.

    [DllImport("kernel32.dll")]
    static extern void RaiseException(uint dwExceptionCode, uint dwExceptionFlags,  uint nNumberOfArguments, IntPtr lpArguments);
    
    void start()
    {
        RaiseException(13, 0, 0, new IntPtr(1));
    }
    

    Happy crashing. Please note that in order to debug native and managed, you will need two instances of Visual Studio running. If you are developing native P/INVOKE plugin, set up it that Visual Studio Instance 1 is native debugger and uses Unity or your C# program as a Host program, and you attach to the Host program from another Visual Studio Instance.

    0 讨论(0)
提交回复
热议问题