This question already has an answer here:

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?

marked as duplicate by gnat, JBRWilkinson, Robbie Dee, 8bittree, Bernard Dy Apr 18 '18 at 21:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


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.


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.