When to Wrap Exceptions

Should I include all exceptions in a more significant exception? The Wrapping value makes the exception an internal exception of the new exception and throws a "new" exception.

What factors do I need to consider when doing this?

Is the idea of ​​wrapping exceptions because:

SQL Server can throw a bunch of exceptions from the T-SQL level. My C # API will only handle SQLException (the descriptor value has a catch block), so I want to wrap the exceptions in a type that my API can handle. This is just an example, SQL Server throws only a SQLException, but is this correct?

I assume that throwing a β€œnew” exception, as mentioned above, will have a reason that is not the real reason (which the developer should know), so it would be more friendly to the end user and hide sensitive implementation information about the real / first exception.

thanks

+6
exception exception-handling
source share
1 answer

You are on the right track. It is good practice to wrap exceptions when they cross application-layer boundaries. The following two posts are well read in the best recommendations.

+6
source share

All Articles