I have inherited code in our project that looks like this. This is a class method.
protected override bool Load()
{
DataAccess.SomeEntity record;
try
{
record = _repository.Get(t => t.ID.Equals(ID));
if (record == null)
{
throw new InvalidOperationException("failed to initialize the object.");
}
else
{
this.ID = record.ID;
}
}
catch (Exception)
{
throw;
}
return true;
}
If I then call this Load method from my user interface level, I would probably want to have a catch try block to catch any exception caused by instance loading failure, for example. InvalidOperationException, but the code above seems wrong to me.
Would an InvalidOperationException be a catch expression? this catch statement will also catch potential problems with _repository.Get, as well as potential problems with setting properties if the record is valid.
, , , , catch catch Get catch, , , , catch , :
protected override bool Load()
{
DataAccess.SomeEntity record;
record = _repository.Get(t => t.ID.Equals(ID));
if (record == null)
{
throw new InvalidOperationException("failed to initialize the object.");
}
else
{
this.ID = record.ID;
}
return true;
}
, , , .