I'm writing a library which will be a base for a turn-based battle system but i don't know what are the best practices for exception handling, has anybody some advice for me?
Should I catch every generic exceptions/ exceptions thrown by standard components (like lists,dictionaries,etc…) and then throw a contextualized exception?
If I have public method that throws an exception, when I call it from another library method should i catch the exception and then re-throw it? And if the method is private?
EDIT: I'm sorry I didn't mention that I throw exceptions only when there are some bugs
Best Answer
First, this will be an opinion, so many reasonable answers will be downvoted by those who have different opinions. Second, it will be somewhat language and platform dependent: an approach that should be slightly better but is incredibly clunky in the language is a bad approach.
The most important choice is to have an understandable plan and communicate it. You probably are thinking of these types of errors:
I recommend you pick and document a naming scheme for problems so that a developer knows what type of exception is passed upwards.