tags:

views:

140

answers:

4

I have a class with number of methods and want to have one exception handler for them all. There are so many of these methods and they have different parameters, that it would be ugly to write try/catch for each of them.

Do you maybe know a way where I can do it with having a one in class exception handler, which will handle them all.

UPDATE:


Many of you ask me why. The reason is that I am calling a data source with various methods. so my class has functions getData1, gedData2, getData3,getData4, ...., getDataN. The problem is that there is no way to check if the connection is still open and creating new connection is very very expensive. So I am trying to reuse connection and if the connection on the next call has failed, i would catch this and reconnect and try again. That is why i need this try/catch all block.

to do this for all the functions:

try{    
   datasource.getData()
}
catch(ConnectionException)
{
   datasource.Connect();
   datasource.getData()
}


Thanks

+1  A: 

I don't think there is. You could move the try/catch to the caller, but that's not very good design. It may be better to separate it out into another class, and use Reflection to call the methods, like this:

public class MyClass {}
public class MySafeClass {
    public void CallMethod(string name, object[] param) {
        Type t = typeof(MyClass);
        MyClass mc = new MyClass();
        try {
            t.GetMethod(name).Invoke(mc, param);
        }
        catch {
            //...;
        }
    }

}

But you shouldn't! It's not very good practice.

Another method is still using try/catch but having a single method to throw exceptions, etc back to the user:

public class MyClass {
    void DoException(string message) {
        throw new Exception(message);
    }
}

But that still isn't that good an option.

I don't see why it would be ugly - even if you just wrap the whole method in one try/catch with a message. That might be feasible.

It's also a better option to just leave them and pass them back up to the caller, perhaps in try/finally.

It's not exactly hard to try/catch everything, especially with the snippets in Visual Studio and SharpDevelop.

Lucas Jones
Thanks for reply. Maybe looking into the update to the question you could see my reasons.
husayt
+1  A: 

This sounds like a problem with your design. Can you elaborate on exactly what exceptions you are trying to catch and why and we can try and help with that.

Kieran Benton
yes probably I need to give more context, check my update to the question please.
husayt
A: 

you might want to put a try/catch about you main method, although i consider this to be a serious misuse of exception handling, except you want to append a logger or sth.

Johannes Rudolph
i need something only for that class and handled inside that classThnks
husayt
+2  A: 

I can't figure out any reason why you may benefit from handling all the exceptions in a class using a single method (can you elaborate? I'm curious...)

Anyway, you can use AOP (Aspect Oriented Programming) techniques to inject (statically or at runtime) exception handling code around the methods of your class.

There's a good assembly post-processing library called PostSharp that you can configure using attributes on the methods in your class:

You may define an aspect like this (from PostSharp website):

public class ExceptionDialogAttribute : OnExceptionAspect
{
    public override void OnException(MethodExecutionEventArgs eventArgs)
    {
        string message = eventArgs.Exception.Message;
        MessageBox.Show(message, "Exception");
        eventArgs.FlowBehavior = FlowBehavior.Continue;
    }
}

And then you'll apply the attribute to the methods you want to watch for exceptions, like this:

public class YourClass {

    // ...

    [ExceptionDialog]
    public string DoSomething(int param) {
        // ...
    }
}

You can also apply the attribute to the whole class, like this:

[ExceptionDialog]
public class YourClass {
    // ...
    public string DoSomething(int param) {
        // ...
    }
    public string DoSomethingElse(int param) {
        // ...
    }
}

This will apply the advice (the exception handling code) to every method in the class.

Utaal