Skip to main content
3 of 5
Removed monogame tag as it does not pretain to the question.

DivideByZeroException is only thrown in Mono builds, not in IL2CPP. Why?

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)) { Debug.Log("L"); var x = 0; var y = 1 / x; } if (Input.GetKeyDown(KeyCode.P)) { Debug.Log("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?

Tudvari
  • 801
  • 16
  • 48