|
-
Sep 18th, 2006, 04:01 PM
#1
Thread Starter
Banned
[02/03] Exception parent child relationships
At the highest level of an exception we have the notorious Exception class..beneath that we have a wide array of classes such as IOException, IndexOutOf..Exception, etc etc.
What is the best way to find out what the first exception an object may throw, for instance is it just best to use the help documents and read on what the object might throw.
Meaning avoiding to have a try / catch / finally block with all the catches being of type Exception e...I would like to be able to throw the particular exception that my code has thrown. For instance, when dealing with File I/O
instead of the General Exception one can throw IOExceptions...
So how does one tell what exceptions specific objects throw that way we can handle them more appropriately ?
Thanks
-
Sep 18th, 2006, 06:52 PM
#2
Re: [02/03] Exception parent child relationships
Objects don't throw exceptions. Methods throw exceptions and the help topic for every method will tell you what exceptions it can throw. If you want to throw exceptions from your own code then the .NET convention is to define your own exception classes derived from the ApplicationException class, which is what distinguishes exceptions thrown by your application rfom exceptions thrown by the system. If you want to catch an exception thrown by the system and then throw your own exception you should assign the system exception to the InnerException property of your own exception object. The only reason to specify a type other than Exception in a Catch block is if you want to handle different types of exceptions differently. If that's what you want to do then by all means do so, but if you don't intend to do anything different for each type of exception then being more specific than the Exception type serves no useful purpose. If you don't need to handle different types of exceptions differently then don't just do so because you can. You can log any exception through the Exception class and its properties. Generally the user doesn't need very specific information. Having said this, there certainly are situations where it's appropriate to handle different exceptions differently. I'm just saying that there's no point making your app more complex than is necessary.
Last edited by jmcilhinney; Sep 18th, 2006 at 06:55 PM.
-
Sep 18th, 2006, 09:29 PM
#3
Re: [02/03] Exception parent child relationships
Very good points, Jmcilhinney.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|