RE: Custom exception?
>I have now seen the error in my ways. Cheers No! You're not really in error. It sounds like you're just over-engineering a bit, which is a questionable sin. Greg
Re: Custom exception?
Hi Greg, you've hit the nail right on the head. I always tend to create "black box" classes even when they are specific to a single project which complicates things and wastes my time. I have now seen the error in my ways. Cheers Thanks everyone for the help! --Bec-- On Thu, Jun 10, 2010 at 9:43 AM, Greg Keogh wrote: > Bec, you seem to own the code that makes the report and it’s a part of your > project, it doesn’t sound like a reusable “black box” library. I’d treat > each of these cases differently. The Framework Design Guidelines book > explains all of this clearly, and I highly recommend everyone in here keep a > copy by their bedside table. > > > > Since your are the caller and author I’d say you have the right to catch the > two exceptions you mentions and deal with them (just don’t catch Exception > and swallow). > > > > However, if this was a reusable library I would tend to create something > like a ReportGenerationException which callers can always trust to catch > from calling the library methods. This would give you a more generic and > friendly error message about what went wrong, but I would still have the > original as the InnerException. And the reusable library must not swallow > exceptions either. Never catch anything unless you know exactly what you’re > going to do with it. > > > > Greg
RE: Custom exception?
Bec, you seem to own the code that makes the report and it's a part of your project, it doesn't sound like a reusable "black box" library. I'd treat each of these cases differently. The Framework Design Guidelines book explains all of this clearly, and I highly recommend everyone in here keep a copy by their bedside table. Since your are the caller and author I'd say you have the right to catch the two exceptions you mentions and deal with them (just don't catch Exception and swallow). However, if this was a reusable library I would tend to create something like a ReportGenerationException which callers can always trust to catch from calling the library methods. This would give you a more generic and friendly error message about what went wrong, but I would still have the original as the InnerException. And the reusable library must not swallow exceptions either. Never catch anything unless you know exactly what you're going to do with it. Greg
Re: Custom exception?
I just wanted to throw in Krzysztof Cwalina s blog Check his post on Choosing the Right Type of Exception to Throw http://blogs.msdn.com/b/kcwalina/archive/2005/03/16/396787.aspx .peter.gfader. http://blog.gfader.com/ http://twitter.com/peitor On Wed, Jun 9, 2010 at 4:04 PM, wrote: > The general rule is > 1. if you can't handle the exception don't. > 2. If you are doing business logic throw a custom exception for when you > need to fail a process. > 3. Inherit from exception and do not use application exception. > > I think your code falls into rule 1. > It's up to the ui to catch and decide if they want to let the user try > another directory. > > Davy. > --Original Message-- > From: Bec Carter > Sender: ozdotnet-boun...@ozdotnet.com > To: ozDotNet > ReplyTo: ozDotNet > Subject: Custom exception? > Sent: 9 Jun 2010 03:59 > > 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? > > Cheers. --Bec-- > > > BBM pin:2589AEE0
Re: Custom exception?
The general rule is 1. if you can't handle the exception don't. 2. If you are doing business logic throw a custom exception for when you need to fail a process. 3. Inherit from exception and do not use application exception. I think your code falls into rule 1. It's up to the ui to catch and decide if they want to let the user try another directory. Davy. --Original Message-- From: Bec Carter Sender: ozdotnet-boun...@ozdotnet.com To: ozDotNet ReplyTo: ozDotNet Subject: Custom exception? Sent: 9 Jun 2010 03:59 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? Cheers. --Bec-- BBM pin:2589AEE0
Re: Custom exception?
On Wed, Jun 9, 2010 at 11:59 AM, Bec Carter 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/
Re: Custom exception?
Yes, no doubt on this one - always wrap the original exception inside the custom one On Wed, Jun 9, 2010 at 1:13 PM, wrote: > Note that if you use the latter approach then you should always include > the original exception as an inner exception to the > ReportGenerationException. > > It's very frustrating to debug an application and get a general > exception that doesn't include all the information you need or at least > could have available simply because the developer decided to throw away > information for no good reason (and I don't think there is ever a good > reason not to include the inner exception - if no one cares then they > don't have to look at it, and if they do they have a full stack trace > and details of the *original* exception). > > Ben > > -Original Message- > From: ozdotnet-boun...@ozdotnet.com > [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of David Kean > Sent: Wednesday, 9 June 2010 10:03 AM > To: ozDotNet > Subject: RE: Custom exception? > > Does the user of the class specify the directory? Or is this hidden from > them? If the former, it's entirely appropriate to throw IOExceptions > because that's what they would expect. If the later, then throw the > custom exception. > > -Original Message- > From: ozdotnet-boun...@ozdotnet.com > [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of Bec Carter > Sent: Tuesday, June 08, 2010 7:00 PM > To: ozDotNet > Subject: Custom exception? > > 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? > > Cheers. --Bec-- > > > This email is intended for the named recipient only. The information it > contains may be confidential or commercially sensitive. If you are not the > intended recipient you must not reproduce or distribute any part of this > email, disclose its contents to any other party, or take any action in > reliance on it. If you have received this email in error, please contact the > sender immediately and delete the message from your computer. >
RE: Custom exception?
Note that if you use the latter approach then you should always include the original exception as an inner exception to the ReportGenerationException. It's very frustrating to debug an application and get a general exception that doesn't include all the information you need or at least could have available simply because the developer decided to throw away information for no good reason (and I don't think there is ever a good reason not to include the inner exception - if no one cares then they don't have to look at it, and if they do they have a full stack trace and details of the *original* exception). Ben -Original Message- From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of David Kean Sent: Wednesday, 9 June 2010 10:03 AM To: ozDotNet Subject: RE: Custom exception? Does the user of the class specify the directory? Or is this hidden from them? If the former, it's entirely appropriate to throw IOExceptions because that's what they would expect. If the later, then throw the custom exception. -Original Message- From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of Bec Carter Sent: Tuesday, June 08, 2010 7:00 PM To: ozDotNet Subject: Custom exception? 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? Cheers. --Bec-- This email is intended for the named recipient only. The information it contains may be confidential or commercially sensitive. If you are not the intended recipient you must not reproduce or distribute any part of this email, disclose its contents to any other party, or take any action in reliance on it. If you have received this email in error, please contact the sender immediately and delete the message from your computer.
Re: Custom exception?
Well actually its currently the former, the caller passes in the temp directory for the report generator to use. I did it like this because the report generator is a separate assembly (a class library) so the caller reads the configured value from the app.config and passes it in to the library. Is this not the way to go maybe? On Wed, Jun 9, 2010 at 12:06 PM, David Connors wrote: > On 9 June 2010 11:59, Bec Carter wrote: >> >> 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? > > In the interests of lower module coupling & abstraction, I would always > choose the latter. The fact that you need to create directories to do > whatever is (well, should be I would think) pretty irrelevant to the caller > (which might be an ASPX or something that wants to show the report to the > end user). > In the future you might choose to rewrite your class to work off a DB, cloud > storage, or something else. > -- > David Connors (da...@codify.com) > Software Engineer > Codify Pty Ltd - www.codify.com > Phone: +61 (7) 3210 6268 | Facsimile: +61 (7) 3210 6269 | Mobile: +61 417 > 189 363 > V-Card: https://www.codify.com/cards/davidconnors > Address Info: https://www.codify.com/contact > >
RE: Custom exception?
Hi Bec, I try to approach these problems pragmatically. I'm unaware of your development environment, deadlines etc. So, pragmatically I say just to do the bare minimum to start with. For me that would be to let the caller of the class handle the exceptions they care about. Once you have some feedback/experience then you can respond to it later on. Practically, for me that means that the caller would handle the exceptions that they know about and let the application fail hard and fail fast for any unknown exceptions. I haven't needed to create a custom exception yet. I have found that Microsoft have provided me with all the exceptions I need e.g. Application exception. I hope this helps. Regards, Michael O'Dea-Jones -Original Message- From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of Bec Carter Sent: Wednesday, 9 June 2010 12:00 PM To: ozDotNet Subject: Custom exception? 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? Cheers. --Bec--
Re: Custom exception?
On 9 June 2010 11:59, Bec Carter wrote: > 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? > In the interests of lower module coupling & abstraction, I would always choose the latter. The fact that you need to create directories to do whatever is (well, should be I would think) pretty irrelevant to the caller (which might be an ASPX or something that wants to show the report to the end user). In the future you might choose to rewrite your class to work off a DB, cloud storage, or something else. -- David Connors (da...@codify.com) Software Engineer Codify Pty Ltd - www.codify.com Phone: +61 (7) 3210 6268 | Facsimile: +61 (7) 3210 6269 | Mobile: +61 417 189 363 V-Card: https://www.codify.com/cards/davidconnors Address Info: https://www.codify.com/contact
RE: Custom exception?
Does the user of the class specify the directory? Or is this hidden from them? If the former, it's entirely appropriate to throw IOExceptions because that's what they would expect. If the later, then throw the custom exception. -Original Message- From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of Bec Carter Sent: Tuesday, June 08, 2010 7:00 PM To: ozDotNet Subject: Custom exception? 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? Cheers. --Bec--