March 2006 - Posts

Implement 'On Error Resume Next'-like behavior in .NET 2.0

'On Error Resume Next'-like behavior using Anonymous Delegates in C# 2005

That's just what an good old VB6 developer likes...

static class IgnoreExceptionsSample
{

 
public void Main()
 
{
   
IgnoreExceptions(
delegate { DoSomething(); });
 
}

 
public static void IgnoreExceptions(SimpleDelegate del)
 
{
   
try
   
{
     
del();
   
}
   
catch { }
 
}


}

public delegate void SimpleDelegate();

Important Notice: When an exception is thrown the exception will be ignored, but the delegate will not be executed further (in contrast to VB6 'On Error Resume Next' which continued with the next statement).

Pretty cool, isn't it?

with 0 Comments