On Wed, Jun 9, 2010 at 11:59 AM, Bec Carter <[email protected]> wrote: > > Hi! > From the more experienced programmers here, when is it appropriate to > create custom exceptions? > I am finding a mix of opinions around. > > eg. I have a class which generates reports. Part of the process is to > create a directory if it does not already exist. If the create > directory fails several types of exceptions can be thrown like > System.UnauthorizedAccessException, DirectoryNotFoundException and so > on. Should the caller of this class care about all of these or should > they just worry about catching a ReportGenerationException which tells > them exactly what went wrong?
My opinion would be that it really only matters if you're handing exceptions out to a third party. Possibly within your own company, possibly even just to a completely isolated component in your own project, but I rarely if ever actually create my own exceptions. It depends, of course, on your exception handling strategy. Typically, most exceptions don't make it very far in the code I write. I'll generally transform them into something else within the certain area they operate (i.e. a "false" response, or whatever). So when I do it, I would really question what the caller will be doing with this information. If they can do something very useful with it, then do it, But otherwise, if it is perhaps better to return something more meaningful (practically useful) that would be - and is - my peferred approach. > Cheers. --Bec-- -- silky http://www.programmingbranch.com/
