여러 프로젝트에서이 문제가 몇 번이나 발생했습니다. 일반적으로 사용하는 것보다 더 나은 솔루션이 있는지 궁금해했습니다.오류 처리를위한 디자인 패턴
우리는 실행해야 할 일련의 메소드를 가지고 있으며, 메소드 중 하나에서 문제가 발생했는지 알기를 원한다면 (이전의 변경 사항을 취소 할 수 있습니다 ...), 일반적으로 난 그냥 궁금
private bool SomeMethod()
{
bool success = true;
string errorMessage = null;
success = TestPartA(ref errorMessage);
if (success)
{
success = TestPartB(ref errorMessage);
}
if (success)
{
success = TestPartC(ref errorMessage);
}
if (success)
{
success = TestPartD(ref errorMessage);
}
//... some further tests: display the error message somehow, then:
return success;
}
private bool TestPartA(ref string errorMessage)
{
// Do some testing...
if (somethingBadHappens)
{
errorMessage = "The error that happens";
return false;
}
return true;
}
(이 내 질문) 이런 종류의 극복을위한 더 나은 방법이 있다면 다음 (의사 C#을 내가 가장 익숙한 해요 무엇 때문에). 나는 그것이 더 매끄러 워야하는 것처럼 보이는 무언가를 위해 많은 if
진술을 쓰는 것을 끝내는 것처럼 보인다.
나는 델리게이트 함수 집합에 대한 반복을 제안했지만, 그것을 할 수있는 확실한 방법이 없다면 솔루션을 과도하게 설계 할 것이라고 걱정할 것입니다.
정말 오류 인 경우 예외가 발생해야합니다. – Polyfun