Your question (why the compiler complains) has already been answered by others. I would, however, suggest you reconsider your design:
bool isOK = SomeClassWithBusinessRules.VerifySomeStuff(idOfStuffToVerify, ref errorMessage); if (!isOK) throw new BusinessException(errorMessage ?? "Some error occured."); }
Since errorMessage is only required when an error occurs, why do you need an additional return value? You can shorten this to:
string error = SomeClassWithBusinessRules.VerifySomeStuff(idOfStuffToVerify); if (error != null) throw new BusinessException(error); }
(Of course, then you no longer have a case with an โerror.โ But displaying โuselessโ error messages is bad practice.)
In fact, if the error is an exceptional circumstance (i.e., something that is not part of the regular control flow, but rather something that indicates a data or logic error), it makes sense to move the exception inside VerifySomeStuff:
// no return value SomeClassWithBusinessRules.VerifySomeStuff(idOfStuffToVerify); class SomeClassWithBusinessRules { void VerifySomeStuff(int id) { ... if (someCondition) throw new BusinessException(error); ... } }
source share