What's a good global exception handling strategy for Unity3D?

前端 未结 5 2050
悲&欢浪女
悲&欢浪女 2021-02-01 04:40

I\'m looking into doing some Unity3D scripting stuff, and I\'d like to set up global exception handling system. This is not for running in the release version of the game, the i

5条回答
  •  無奈伤痛
    2021-02-01 04:56

    You mentioned Application.RegisterLogCallback, have you tried implementing it? Because the logging callback passes back a stack trace, an error, and an error type (warning, error, etc).

    The strategy you outline above would be tough to implement because MonoBehaviours don't just have a single entry point. You'd have to handle OnTriggerEvent, OnCollisionEvent, OnGUI, and so on. Each one wrapping its logic in an exception handler.

    IMHO, exception handling is a bad idea here. If you don't immediately re-throw the exception, you'll end up propagating those errors in weird ways. Maybe Foo relies on Bar, and Bar on Baz. Say Baz throws an exception that is caught and logged. Then Bar throws an exception because the value it needs from Baz is incorrect. Finally Foo throws an exception because the value it was getting from Bar is invalid.

提交回复
热议问题