advise about handling socket exceptions with EHAB

Jul 6, 2009 at 3:53 PM


I will describe how my application is currently set up and then explain the problem I am faced with.

In my application all i want to do is log an error when it happens. I don't want to rethrow or anything like that. In my application i want to store different information in the exception depending on what exception was thrown. For example if its a socket exception i add the socket exception error code to the Data collection of the exception object and if there is a problem with the input data to the application i store the input data in the data collection.

So my application has try/catch finally statements that look like this


catch(socketexception e)

   //get socket error code and add to e

<font size="2">




catch(exception e)

 //get input data for application and add to e

ExceptionPolicy.HandleException(e, "LoggingPolicy")

Being as i want the same outcome for each application type in the exception handling application block i created one exception policy called LoggingPolicy with one Exception type added for Exception which has a Category called General with one TraceListener associated with it. This set up logs the messages for all exceptions that are generated without me having to add multiple exceptions to the exception policy. I think this is ok? If i could do this better I would welcome advice.

I now have the problem that i need to raise an email if a specific socket error occurs. However with winsock errors you don't get different socket exceptions for the different types of error. You get one socket exception with an error code property that indicates the nature of the error. If i got different socket exceptions for the different types of errors I could just add the relevant socket exception to my existing LOggingPolicy and assign it a logging handler that has a category associated with an email trace listener. But i can't do this (unless someone can tell me different) so the best I can think of is to have a second exception policy. This one has one SocketException which has a logging handler assigned to a Category called email which is linked to an email trace listener. Then in the try catch blocks above i would have to evalute the error code of the socket exception like so

catch(socket exception e)

if e.errorCode is not urgent then




I don't think the EHAB was designed to be used like that but i think its the best I can do. What do you think?


Jul 7, 2009 at 3:54 AM

It's fine but along the way, you may have additional requirements to catch specific type of exceptions.  The result would be you'll be having a lot of logic in your catch statement or even a lot of catch exception statements.  If you don't mind, you could create a custom exceptionhandler wherein you could put all those logic of determining the exact exception which occured and the error code stuff.  You would then configure an exceptionpolicy having this custom handler and your catch statement would now only use this policy.  That policy would now take care of handling your exceptions using the appropriate exception policy.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

Jul 13, 2009 at 10:49 PM


Thanks for your reply. Could you provide me with a bit more detail. This is what i think you mean

1. Create one exception policy that handes Exception

2. Have a custom exception handler attached to this exception policy

3. The exception handler has code which determines the type of the exception and performs appropriate action.  if the exception handler detected an urgent scoket exception it would create a LogMessage of Category urgent, which would be attached to an email trace listener. Otherwise the log message category could be General which is attached to a database listener.

The only thing I'm not sure about is how i could pass in extra information into the exception depending on the error that occured. I wouldn't need to take any extra measures to pass in the error code of the socket code as that will be available within the exception but there is some program input data. I guess i could pass that in with every exception - it will do no harm

thanks in advance for your help

Jul 14, 2009 at 6:25 AM

I missed that part.  If those extra information would always be available, you could just always add it in your catch block.  If not, you could resort to your original plan.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.