I have started writing asserts into my code recently, but have discovered that with both Debug.Assert
and Trace.Assert
, the execution continues if the condition is not met. Is there an inbuilt equivalent that will throw an exception when the condition is not met? Or should I write my own? I want to throw an exception so that I don't continue to write something that is in an unexpected state.
EDIT: I have code somewhat like what is below. This is within a PUT request, so I want to update an existing resource (identified ideally by the Id, but also by a GUID in the case where a partial save occurred previously). If saving a new resource, then I want to ensure that a GUID is supplied.
public int Save(Guid? personGuid, Person person)
{
if (person.Id > 0)
{
Update(person);
}
Trace.Assert(personGuid.HasValue);
SaveToDb(personGuid, person);
}
NB: The Define TRACE constant
option is checked.