C# Throw Exception on use Assert?

Posted by guazz on Stack Overflow See other posts from Stack Overflow or by guazz
Published on 2010-04-27T18:38:22Z Indexed on 2010/04/27 18:43 UTC
Read the original article Hit count: 377

Filed under:
|

I have a system where the employeeId must alway exist unless there is some underlying problem.

The way I see it, is that I have two choices to check this code:

1:

public void GetEmployee(Employee employee)  
{  
   bool exists = EmployeeRepository.VerifyIdExists(Employee.Id);  
   if (!exists)   
   {   
     throw new Exception("Id does not exist);  
   }  
}    

or 2:

public void GetEmployee(Employee employee)  
{  
  EmployeeRepository.AssertIfNotFound(Employee.Id);  
}  

Is option #2 acceptable in the C# language?

I like it because it's tidy in that i don't like looking at "throw new Exception("bla bla bla") type messages outsite the class scope.

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception-handling