clinic

i have a static method :

Code Snippet

public static void myMethod(int AlarmId, IPEndPoint IpEndPoint)

{

try

{

ClientThread.Acknoledge(AlarmId, IpEndPoint);

}

catch (SocketException soe)

{

UnableToConnectEventArgs e = new UnableToConnectEventArgs(soe.Message);

// TODO

if (OnUnableToConnectHandler != null) { OnUnableToConnectHandler(null, e); }

}

}

i have to use an exception system already in place, but i need to give an object to my OnUnableToConnectHandler(object, exception) method...

in a static method, calling a static method too, i a have no object ... So is this an acceptable good practice or not



Re: Visual C# General Best practice concerning intercepting Exception on a static method ?

Peter Ritchie

I think it would be worse practice to simply make a static method an instance method simply to get a this object... I see no problem with passing null.




Re: Visual C# General Best practice concerning intercepting Exception on a static method ?

clinic

Ok, that is what i thought.

So i can consider i can make exception that says : ' Hello, i am an exception, but i have no parent object, please Caught me !'





Re: Visual C# General Best practice concerning intercepting Exception on a static method ?

OmegaMan

clinic wrote:

Ok, that is what i thought.

So i can consider i can make exception that says : ' Hello, i am an exception, but i have no parent object, please Caught me !'



An exception does not have to have a parent object, it is simply a mechanism to inform the consumer that something deleterious has happened. There are numerous Microsoft statics that generate exceptions, see some of the File static methods, and they are documented on what exceptions will be thrown, which is key. Here are your options
  1. Catch no exceptions in the static, but allow the consumer of the static to know that it may need to catch certain exceptions and can plan on it.
  2. The static can catch all exceptions and throw, or even rethrow, one exception back to the consumer, as long as the consumer knows about it...
  3. The static can catch all exceptions, but do not throw anything back, just fail to do the operation and in most cases, send a null as the return value back to the consumer.

Those are your choices and each have pros and cons that you must weight in your design. Good Luck.