I'm working on exception handling and I usually test it with causing a DivideByZeroException.

But this time it's not thrown. (While throwing a System.Exception works just like usual).

So I've created an empty project with only this script:

void Update()
    if (Input.GetKeyDown(KeyCode.L))
        var x = 0;
        var y = 1 / x;
    if (Input.GetKeyDown(KeyCode.P))
        throw new System.Exception("test");

And with the empty project the DivideByZeroException is thrown, so I had a look on the project configurations.

Turns out the new project's Scripting Backend is Mono.

Once I changed it from Mono to IL2CPP, the DivideByZeroException is not thrown in this project as well.

enter image description here

I experimented with changing the API Compability Level, but they behave the same.

enter image description here

So last time I debugged exception handling, I was on Mono and then I changed it ... I guess to make it faster or something.

My question is: why is this the case?

  • 3
    \$\begingroup\$ Wild guess: both variables get optimized out because they are not used \$\endgroup\$
    – user253751
    May 6 at 12:45
  • \$\begingroup\$ @user253751 Yes, you are probably right. I added a line: gameObject.name = y.ToString() so it's now used. Now it crashes when I press L. So yes, probably it was removed due to not being used. But then my followup question is: Why doesn't it just throw an exception? Instead of crashing alltogether? The System.Exception throwing hotkey still works just as it should: I press the key -> dev console appears with exception. \$\endgroup\$
    – Tudvari
    May 6 at 13:19


You must log in to answer this question.

Browse other questions tagged .