Catching exceptions

danfloun

Member
Joined
Apr 5, 2010
Messages
23
Programming Experience
Beginner
Just a quickie.

If a member of a class such as OpenReadAsync has many possible exception events, should one simply use one catch after another to cover all known exceptions plus unknown ones, in order of inheritance?

OpenReadAsync Exceptions;
ArgumentNullException
OutOfMemoryException
StackOverflowException
ThreadAbortException

Thanks
Danny
 
How to: Filter Errors in a Catch Block in Visual Basic
Insert a Catch statement for each type of exception you wish to check, going from the most specific to the most general.
For example if you were to handle ArgumentNullException and ArgumentException you would have to catch them in in that order, if not the base ArgumentException would catch ArgumentNullException also.

OpenReadAsync method only throws WebException by the way, but I think you meant as a general approach.
 
How to: Filter Errors in a Catch Block in Visual Basic

For example if you were to handle ArgumentNullException and ArgumentException you would have to catch them in in that order, if not the base ArgumentException would catch ArgumentNullException also.

OpenReadAsync method only throws WebException by the way, but I think you meant as a general approach.

Hi,

Thanks for the link, not sure how I missed that lol.

Anyway I think I was on the right track, I said in order or inheritance I actually meant what you said.... most specific first. I found that out myself by having two catch statements in the wrong order. think I had ex as Exception first.

Yes, I was generalizing and wasn't being specific about OpenReadAsync.

Thanks for your info.

Danny
 
I like exceptions; I don't use them, I try to write code that doesn't require exception handling.

I write one try function in my code. It's in the cycle loop of the program.
Try
' RUN MY CODE
CATCH ex as exception
' CODE IS BROKE FIX IT
END TRY

:)

Yes though you were correct; It's from Narrow to Widening.
 
I like exceptions; I don't use them, I try to write code that doesn't require exception handling.

I write one try function in my code. It's in the cycle loop of the program.
Try
' RUN MY CODE
CATCH ex as exception
' CODE IS BROKE FIX IT
END TRY

:)

Yes though you were correct; It's from Narrow to Widening.
Well write code like that, although some exceptions simply can't be avoided, especially ones related to IO or Network operations. But in general I write code in which an exception can't be raised so I therefor don't need a try/catch at all.
 
I agree; as a recommendation to developers. I would suggest that you try not to use TRY CATCH if at all possible; for two reasons :

First - it is expensive in process; most of the time when developers use them they are just being too lazy to put a condition statement some where and the TRY block does moer Operations then a simple IF compare. Such as :

VB.NET:
If Not SomeVar is Nothing Then
End If

That is the most common coding technique I have seen with Try/Catch subsitute

Second - It encourages lazy programming; seriously it is way to easy to just wrap a TRY CATCH block, and ignore actual thinking through the problem in code. I have literally seen programs litered with TRY CATCH and no IF conditions; that was very hard to read and follow.

I am with you on that JuggaloBrotha 100%.


TRY is for Rapid Application Development or simple function testing and should always be reviewed for more efficent operations.



Although, I have a question about Network Ops, Would it be better to use the Try Block or just do intermidant testing in a timed function?
 
Back
Top