返回flag

//修改前
namespace CleanCSharp.Errors.Dirty
{
public class SomeClass
{
public int DoSomeProcess(int? id)
{
if (id == null)
{
return -; // null id
}
string data = LoadData();
if (string.IsNullOrWhiteSpace(data))
{
return -; // data is corrupt
}
ProcessData(data); return ; // no error, all good
}
private string LoadData()
{
return "some data";
}
private void ProcessData(string data)
{
// do something
}
}
}
修改前,调用
namespace CleanCSharp.Errors.Dirty
{
public class ConsumerOfSomeClass
{
public void Consume()
{
var sc = new SomeClass();
const int idToProcess = ;
int returnCode = sc.DoSomeProcess(idToProcess);
switch (returnCode)
{
case -: // null id
// do something break;
case -: // corrupt data
// do something
break;
case : // no error
Save(idToProcess);
break;
}
}
private void Save(int id)
{
// save
}
}
}
//修改后
using System;
using System.IO;
namespace CleanCSharp.Errors.Clean
{
public class SomeClass
{
public void DoSomeProcess(int? id)
{
if (id == null)
{
throw new ArgumentNullException("id");
}
string data = LoadData();
ProcessData(data);
}
private string LoadData()
{
var demoData = "";
if (string.IsNullOrWhiteSpace(demoData))
{
throw new
InvalidDataException(
"The data stream contains no data.");
}
return demoData;
}
private void ProcessData(string data)
{
// do something
}
}
}
//修改后
using System;
using System.Diagnostics;
using System.IO;
namespace CleanCSharp.Errors.Clean
{
public class ConsumerOfSomeClass
{
public void Consume()
{
var sc = new SomeClass();
const int idToProcess = ;
try
{
sc.DoSomeProcess(idToProcess);
}
catch (ArgumentNullException ex)
{
// null id // do something such as logging
// if cannot respond to this
// exception propagate up the
// call stack
throw;
// Notice the throw is not: throw ex;
}
catch (InvalidDataException ex)
{
// bad data
// do something
throw;
}
catch (Exception ex)
{
// any other exceptions that may occur
// do something
throw;
}
Save(idToProcess);
}
private void Save(int id)
{
// save
}
} }
上一篇:记录linux系统用户shell终端操作记录


下一篇:ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation