Best practices for exception management in Java or C# [closed]

It seems odd to me that you want to catch exceptions and turn them into error codes. Why do you think the caller would prefer error codes over exceptions when the latter is the default in both Java and C#?

As for your questions:

  1. You should only catch exceptions that you can actually handle. Just
    catching exceptions is not the right thing to do in most cases.
    There are a few exceptions (e.g. logging and marshalling exceptions
    between threads) but even for those cases you should generally
    rethrow the exceptions.
  2. You should definitely not have a lot of try/catch statements in your
    code. Again, the idea is to only catch exceptions you can handle.
    You may include a topmost exception handler to turn any unhandled
    exceptions into something somewhat useful for the end user but
    otherwise you should not try to catch each and every exception in
    every possible place.

Leave a Comment