0

I'm developing a game engine in C# and I just had a question about handling exceptions correctly.

Since the engine will be used by other .NET developers, how should I handle exceptions?

What I mean is, in any other application that may be used by people outside of the development if an exception is caught, the program will try to correct it and continue running. However, with a game engine (or anything that will be used by other developers), it seems to make more sense to just throw an exception to point out to the user that they're doing something wrong, rather than just fixing it if needed.

Does this seem like a good way to approach handling exception in this case?

2 Answers2

3

If you can handle it appropriately yourself, then handle it. Otherwise, let the caller handle the thrown exception as they see fit. It really depends on what you intend to expose through your game engine and whether there are exceptional circumstances you want to handle instead of letting the engine's users handle it themselves.

Bernard
  • 8,859
  • 31
  • 40
2

When you write library/api for other developers, can be very helpful to provide more information about thrown exception.

For example, instead of getting ArgumentException from some internal method, you can check and throw exception early with descriptive message.
Or even better introduce own Exception types with comprehensive names.

Handle exceptions by yourself only in cases when exception is not exception anymore in the logic of your engine.
For example you want allow consumer to devide by 0 - then you need to handle DivideByZeroException and return some proper result to the consumer of your engine.

Fabio
  • 3,086
  • 1
  • 17
  • 25