Share via


예외 문제 해결: System.FormatException

FormatException 예외는 인수 형식이 메서드의 매개 변수 사양에 맞지 않을 때 메서드에서 throw합니다.

예를 들어 System 네임스페이스에 정의된 대부분의 데이터 형식에는 문자열 인수를 사용하고 이를 데이터 형식으로 변환하는 Parse 메서드가 있습니다. 이러한 메서드는 제공된 인수가 변환할 수 있는 형식이 아닌 경우 FormatException을 throw합니다. Double.Parse는 해당 문자열 인수가 인식할 수 있는 숫자 형식이 아닌 경우 FormatException을 throw합니다. 다음 예제를 살펴보십시오.

' The first three statements run correctly.

Console.WriteLine(Double.Parse("32,115"))

Console.WriteLine(Double.Parse("32115"))

Console.WriteLine(Double.Parse("32.115"))

' The following statement throws a FormatException.

' Console.WriteLine(Double.Parse("32 115"))

마찬가지로 Boolean.Parse는 문자열 인수가 "True" 또는 "False"가 아닌 경우 이 예외를 throw합니다.

' This statement runs correctly.

Console.WriteLine(Boolean.Parse("True"))

' This statement throws a FormatException.

' Console.WriteLine(Boolean.Parse("Ture"))

관련 팁

  • 메서드 인수의 형식이 올바른지 확인하십시오.
    메서드 인수의 형식은 호출된 멤버의 매개 변수 사양과 일치해야 합니다.

참고 항목

작업

방법: 예외 도우미 사용

참조

FormatException

Double.Parse

Boolean.Parse