Ignoring number overflow exception

Is there a way to ignore the number overflow exception? I’m doing some calculations for security on ints, and I am counting on them overflowing and switching from negative to positive and what not. However, it just returns an error in the editor “OverflowException: Number Overflow”.

I think ignoring overflow exceptions requires a compiler flag, which seems like it wouldn’t be possible in Unity. You can use try/catch to handle it instead though.

–Eric

Coincidentally, while looking for something entirely different, I just happened to find out I was quite wrong about that. You can use the “unchecked” keyword (C# only, apparently):

using UnityEngine;

public class Test : MonoBehaviour {
	void Start () {
		unchecked {
			var foo = int.MaxValue;
			var bar = foo+5;
			print (bar);
		}
	}
}

There’s also a “checked” keyword too, but that just results in the default Unity behavior.

–Eric