I’ve got a socket wrapper library I’ve been working on that handles, among other things, a little handshake at the beginning to ensure whoever you’re talking to is using the same protocol and settings as you. I created my own exception class the standard way:
private class NSocketProtocolException : System.Exception
{
public NSocketProtocolException(string message) : base(message) { }
}
I throw this exception for a variety of reasons, like, if the client and server aren’t using the same version of the protocol, if one wants to use encryption and the other doesn’t, etc. Right now I’m the only one who uses this library, but I was trying to design it to be friendly enough so that maybe in the future other people could. In order for other programmers to be able to do something more worthwhile with the different instances where the exception would be thrown, should I have my own custom error codes to go with these?
I don’t really know how to edit the error code, but more importantly, I don’t want to use the same error code that maybe another type of exception uses. Any thoughts?
You don’t need error codes, exception Message is the place for the details about exception. If you use error codes you will have to still document that error codes somewhere, right ? So that description can be in exception Message. Users of your code can catch exceptions by type.
If you need to store some data about exception or about context, use Exception.Data, for example you could put in data Time Stamp of exception, or in web application you could put current request URL in Data etc.
Also be sure to read this blog post about custom exceptions, do you really need them ?