【发布时间】:2010-04-27 18:38:22
【问题描述】:
我有一个系统,其中employeeId 必须始终存在,除非存在一些潜在问题。
我的看法是,我有两种选择来检查这段代码:
1:
public void GetEmployee(Employee employee)
{
bool exists = EmployeeRepository.VerifyIdExists(Employee.Id);
if (!exists)
{
throw new Exception("Id does not exist");
}
}
或 2:
public void GetEmployee(Employee employee)
{
EmployeeRepository.AssertIfNotFound(Employee.Id);
}
选项 #2 在 C# 语言中是否可接受?
我喜欢它,因为它很整洁,因为我不喜欢在类范围之外查看“throw new Exception("bla bla bla") 类型的消息。
【问题讨论】:
-
为什么不让您的 VerifyIdExists 方法代表您抛出异常?
-
我认为您所拥有的没有任何问题,除了恕我直言,我会将名称更改为 ThrowIfNotFound。我认为这是您希望在发布版本和调试版本中包含的内容。
标签: c# exception-handling