views:

266

answers:

7

I'm looking for an "elegant" way to supress when calling a method.

I think

try
{ CallToMethodThatMayFail(3); }
catch {}

is way too verbose. Is there some syntactic sugar I can use to say "I don't really care if this method fails"? I want to call the method and continue execution regardless of what happens with the method.

BTW, moving the try inside the method call is not what I'm looking for. Also, ideally, the solution for this would be able to be re-used on any method call. One last thing: it would also be apparent that the call to this method will not care about errors.

+2  A: 

I don't know of anything more terse. I suppose you could do some AOP or similar for something more fancy.

Larsenal
Yeah, something AOPish is what I was thinking too. I just don't know how to actually do it.
Esteban Araya
Something AOPish... http://www.postsharp.org/
Austin Salonen
(-1) "I don't know" doesn't really help. "Suppose you could do" isn't really an answer.
+6  A: 

It is rarely a good idea to ignore/swallow errors...

To allow re-use, the only option you have is something like a method that takes an Action:

 static void IgnoreErrors(Action action) {try {action();} catch {}}

But you haven't exactly saved much by the time you've done:

SomeHelper.IgnoreErrors(() => CallToMethodThatMayFail(3));

I'd just leave the try/catch in place...


Re the question in the comment:

static void IgnoreErrors<T>(Action action) where T : Exception
{
    try { action(); } catch (T) {}
}

SomeHelper.IgnoreErrors<ParseException>(() => CallToMethodThatMayFail(3));

but I would still find it clearer to have the try/catch locally...

Marc Gravell
Is there a way that you could have an "IgnoreError" function that takes in BOTH the function to call and the errortype to ignore?That way you wouldn't ignore system errors, but you'd be able to ignore 'parse errors' if you're iterating through tons of user entered data.
yes... I'll update
Marc Gravell
@Marc: You're right; it's rarely a good idea to do this. However, in the event that you do want to do it, what's the best way to do it? I appreciate the fact that you actually answered the questions. I'm not so appreciatiave of non-answers getting upvotes.
Esteban Araya
I might use the delegate approach if I had a lot of common handling code to run in a few scenarios... but when swallowing? I'd probably just use try { /* call method */ } catch (ExpectedException) {}
Marc Gravell
+5  A: 

Nope this is it.

And it's a good thing it's verbose. If you're suppressing a possible exception you better have a very good reason. The verbosity will help you or the next person who looks at the code in a few months.

nos
With all due respect: I can make the decision about whether I have a good reason to suppress the exception. You're also assuming that this is code that someone else will actually look at.
Esteban Araya
A: 

No, there's no better way to do this, and there's a good reason for it. The exception you see may mean more than "the method failed". It may mean, "the system is failing".

You probably should at least log the fact of the failure, in case it turned out to be important after all.

John Saunders
+2  A: 

Using Castle, you could do something like this:

public class ExceptionSuppressionInterceptor : Castle.Core.Interceptor.IInterceptor
{
   public void Intercept(IInvocation invocation)
   {
       try {
           invocation.Proceed();
       }
       catch (Exception ex) {
            // Suppressed!
       }
   }
}

And decorate the class you want to suppress exceptions for like this:

[Interceptor(typeof(ExceptionSuppressionInterceptor))]
public class GoodPracticeBreaker {

}

But you really probably shouldn't.

Jeremy Frey
Good practice breaker! :)
Groo
A: 

Why do you say this is too verbose? If you are trying to save keystrokes, you can just use a code snippet. If you are concerned about readability, then I think that an AOP method would be less clear to another maintainer (at least initially) and for me, this outweighs the verbosity.

+1  A: 

The .Net convention is that classes implement a TryCallMayFail() method and a CallMayFail() method and the caller chooses which one to uses but the TryCallMayFail() method would include exactly what you have there.

SpaceghostAli
All classes? All methods?
John Saunders
Not not all but a few of them do and its a style I've become accustom to
SpaceghostAli