Do

Before

if (data != null)
{
  Console.WriteLine(data);
}

After

data.Do(d=>Console.WriteLine(d));

With

Before

string workPhoneCode;

if (person != null)
{
  if (person.Work != null)
  {
    if (person.Work.Phone != null)
    {
       workPhoneCode = person.Work.Phone.Code;
    }
  }
}

After

string workPhoneCode = person.With(p=>p.Work).With(w=>w.Phone).With(p=>p.Code);

Return

Before

string workPhoneCode = "default code";

if (person != null)
{
  if (person.Work != null)
  {
    if (person.Work.Phone != null)
    {
       workPhoneCode = person.Work.Phone.Code;
    }
  }
}

After

string workPhoneCode = person.With(p=>p.Work).With(w=>w.Phone).Return(p=>p.Code, "default code");

If/IfNot

Before

if ((person.Age > 18) &&
   (person.LastName != null) &&
   (person.Work != null))
{
     Console.WriteLine(person);
}

After

person.If(p=>p.Age > 18).IfNot(p=>p.LastName == null).IfNot(p=>p.Work == null).Do(p=>Console.WriteLine(person));

Recover

Before

var person = new Person();

// do something

if (person == null)
{
  person = new Person();
}

Console.WriteLine(person.LastName);

After

var person = new Person();

// do something

Console.WriteLine(person.Recover(()=>new Person()).LastName);

TryDo/Catch

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch
{
}

After

var person = ... ;
// person.Work = null;
person.TryDo(p=>Console.WriteLine(p.Work.Address));


Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch
{
  Console.WriteLine("Error");
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address)).Catch(e=>Console.WriteLine("Error"));

 


Checking exception type (via Type)

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch (NullReferenceException)
{
  Console.WriteLine("Error");
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address), typeof(NullReferenceException)).Catch(e=>Console.WriteLine("Error"));


Checking exception type (via predicate)

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch (NullReferenceException ex)
{
  if (ex.Message.Contains("some word") == false)
  {
    throw;
  }
  Console.WriteLine("Error");
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address), e=>e.Message.Contains("some words")).Catch(e=>Console.WriteLine("Error"));

Ignore exceptions

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  Console.WriteLine(person.Work.Address);
}
catch
{
}

After

person.TryDo(p=>Console.WriteLine(p.Work.Address)).Catch();

TryWith/Catch

Before

var person = ... ;
// person.Work = null;
try
{
  // NullReferenceException?
  var address = person.Work.Address;
  Console.WriteLine (address.TrimLeft());
}
catch
{
}

After

var person = ... ;
// person.Work = null;
person.TryWith(p=>p.Work.Address).Catch().Do(p=>Console.WriteLine(p));

Exception matching and handling policies such as for TryDo.

Last edited Jun 14, 2011 at 7:16 PM by Sergun, version 27

Comments

Arteny Jan 26, 2013 at 8:11 AM 
1. Is there difference between TryDo() and TryDo().Catch()?
2. I have issue with TryDo. Even exception (FileNotFoundException in my case) is ignored in release as it is designed, but in debug mode VS stops execution with dialog "Unhandled user exception" in this place, when try{}catch(){} don't stop same code. Is there a possibility to avoid stop debugger on an exception inside TryDo?