C#/.NET/GUI 프로그래밍의 새로운 기능이지만 여기에 설명되어 있습니다. 지금은 WinForms를 사용하여 업무용 응용 프로그램을 작성하고 있습니다. 나는 약 5 개의 텍스트 상자와 1 개의 콤보 박스를 가지고 있는데, 나는 비어 있는지 확인하고, 그렇다면 사용자에게 말하고 그 컨트롤에 초점을 둔다. 어떻게해야합니까?예외 내에서 컨트롤 캡슐화
내가 가질 수 어느 쪽 각 컨트롤을 확인 문 경우 :
if (usernameField IsNullOrEmpty) then:
setFocus(UsernameField);
return;
if (addressField IsNullOrEmpty) then:
setFocus(addressField);
return;
continue with rest of application as normal...
아니면 예외를 제외하고이 작업을 수행 할 수 있습니다 :
try {
if (usernameField IsNullOrEmpty) then:
throw new Exception(usernameField);
if (addressField IsNullOrEmpty) then:
throw new Exception(addressField);
} catch (Exception e) {
setFocus(ControlField) // encapsulate in exception some way?
}
또는 코드 중복을 방지하기 위해, 단지 함수를 작성 :
try {
checkField(usernameField);
checkField(addressField);
} catch (Exception e) {
setFocus(ControlField) // encapsulate in exception some way?
}
void checkField(control ctrl) {
if (ctrl IsEmptyOrNull)
throw new Exception(ctrl);
}
GUI 프로그래밍에 비교적 익숙하지 않은 텍스트 필드는 예외입니까 아니면 정상적인 프로그램 흐름으로 간주됩니까?
정확히 내가 무엇을 찾고 있었습니까. 그렇다면 예외 처리를받을 자격이없는 것입니까? – keelerjr12
헬퍼 메서드는 알려진 컨트롤의 값을 확인하기 때문에 예외 처리기가 필요하지 않습니다. ctrl이 때때로 null 인 것으로 의심되는 경우 해당 체크를 추가하고 다른 작업을 수행 할 수 있지만 개인적으로 null ref 예외를 남기거나 대신 ArgumentNullException을 throw합니다. –