시작하려면 아마도이 작업을 수행하는 더 좋은 방법이 있다는 것을 깨달았고 예외를 던지기보다는 조건을 더 잘 처리해야합니다. 즉, 나는 예기치 않은 행동에 빠졌고, 이것이 내 응용 프로그램을 사용하는 것보다 더 이상한 지 궁금합니다.if-else 블록에서 예외 던집
한 가지 방법으로 사용자가 제공 한 파일에 액세스하려고 시도하고 있습니다. 그는 메서드의 시작 부분에서 파일 경로가 Null
또는 String.Empty
이 아닌지 확인하고 예외가 발생했는지 확인합니다. 테스트 할 때 조건에 관계없이 예외가 발생한다는 것을 알았습니다. 이 정상적인 행동입니까, 아니면 뭔가 빠졌습니까?
public static XElement Foo(String path)
{
if (String.IsNullOrEmpty(path))
{
throw new ArgumentNullException(); // this exception is thrown
// regardless of the value of 'path'
}
// code to open and parse file
// returns XElement
}
UPDATE : 내 테스트에 처하지에서
가, 호출 방법은 그냥 열심히 테스트에 코딩 된 기본 경로를 보내고있다. UI를 완료하지 않았으므로 경로를 정의하는 코드가 완전하지 않습니다.private const string c_fooPath = "C:\\test\\text.txt"
public void CallingFoo()
{
var xml = Foo(c_fooPath)
// some code
}
업데이트 # 2 :
그냥 내 다른 시험의 일부 내가 시도를 언급. 나는 시도했다if (String.IsNullOrEmpty(path))
{
Console.WriteLine("testing") // this line is skipped when my condition is
// false but runs when i force it to be true
throw new ArgumentNullException(); // this exception is thrown
// regardless of the value of 'path'
}
if (false)
{
throw new ArgumentNullException(); // the exception is not thrown here - the only
// condition i have found so far.
}
public static XElement Foo(String path)
{
path = "test";
if (String.IsNullOrEmpty(path))
{
throw new ArgumentNullException(); // exception is still thrown
}
// code to open and parse file
// returns XElement
}
안녕하세요. Foo 메소드를 호출하십시오. 그러면 더 의미가 있습니다. 또한, String.Empty 및 그렇지 않으면 :). 감사. – Vaibhav
문자열에 데이터가있는 조건을 테스트 했습니까? 이것은 단지 null 또는 empty를 던져 버려야하는 것처럼 보입니다 ... 나에게 맞는 것 같습니다. 문자열을 if 문 바로 앞의 값으로 설정하고 여전히 throw되는지 확인하십시오. – Mikecito
@Vaibhav 무엇이 string.Empty 어떤 컨텍스트에서 이것을 언급하고 있습니까? – V4Vendetta