Defining exceptions that can be thrown from a toolkit API
.net, api, c#, exception
Solution
In my opinion having the API throwing only one exception type is a bad idea. One of the good things with different exceptions is that you can choose to catch different types of exceptions and handle them differently. Wrapping up exceptions into a single exception type would remove that facility.
Use the exception types provided by the framework where appropriate, and create your own custom exception types for specific situations where appropriate. And above all, make sure to document for each method which exceptions they may throw.
Problem
Is there a best-practice or industry standard for throwing exceptions from a toolkit API? Should the user facing methods catch and wrap up `Exception` in some sort of `CustomException` so that users only have to worry about `CustomException`s coming out of the API? Or is the convention to just let those bubble up? We're concerned with being able to document all possible exceptions our API methods might throw. (For example, if our API method calls `Stream.Write()` which throws 4 or 5 exceptions, we'd have to document all of those in addition to other exceptions that other called methods might throw.) We were thinking of doing something like this: ``` public void customerFacingApiMethod(){ try { //api functionality goes here } catch (Exception e) { throw new CustomException(e); } } ```