EConvertError
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
EConvertError means a type conversion failed because the value you tried to convert was not in the expected format. The most common case is trying to convert a string to a number when the string contains letters or is empty. Delphi raises this at runtime through functions like StrToInt(), StrToFloat(), and StrToDateTime().
Affected Models
- All Delphi versions
- RAD Studio
- SysUtils unit functions
Common Causes
- Calling StrToInt() on a string that contains non-numeric characters or is empty
- Calling StrToFloat() on a string that uses a different decimal separator than expected
- Calling StrToDateTime() with a date string that does not match the system's date format
- Data from user input, a file, or a database containing unexpected characters
- Locale differences — a number formatted with a comma as decimal separator failing on a system that expects a period
How to Fix It
-
Replace StrToInt() with TryStrToInt(). TryStrToInt(S, Value) returns True if conversion succeeded and False if it failed — no exception is raised.
The same pattern applies: TryStrToFloat(), TryStrToInt64(), TryStrToDateTime(). These are the safe versions of all the conversion functions.
-
Validate input before converting. For numeric input, check with a regular expression or simple loop that the string contains only digits (and optionally a sign and decimal point).
Quick check: if TryStrToInt(S, N) then use N else show an error message to the user.
-
For date/time conversions, use FormatSettings to specify the exact format expected, or use TryStrToDateTime with explicit format settings.
Dates from external sources often use different separators or ordering (US: mm/dd/yyyy vs European: dd/mm/yyyy).
-
For float conversions, be aware of decimal separators. Some locales use a period (1234.56), others use a comma (1234,56). Use FloatToStrF with a specific FormatSettings to control this.
When reading floats from files or APIs, use a fixed FormatSettings with DecimalSeparator = '.' for predictable behavior.
-
Wrap conversion calls in try/except EConvertError blocks at points where bad input is expected — like processing data from files or external systems.
Example: try Value := StrToInt(S); except on E: EConvertError do ShowMessage('Invalid number: ' + S); end;
When to Call a Professional
EConvertError is always something you can fix yourself. Use the TryStrToXxx family of functions to convert without raising exceptions. Always validate data from user input or external sources before converting.
Frequently Asked Questions
What is the difference between StrToInt and TryStrToInt?
StrToInt raises EConvertError if the string is not a valid integer. TryStrToInt returns True or False and puts the result in an output parameter. Use StrToInt only when you are certain the string is valid (like a constant or a value you just validated). Use TryStrToInt whenever the input comes from users or external data.
Why does StrToFloat fail on some computers but not others?
StrToFloat uses the system's locale settings to determine the decimal separator. On a computer with a US locale, it expects a period (1.5). On a computer with a European locale, it expects a comma (1,5). To avoid this problem, use a fixed FormatSettings when converting floats.
How do I convert a number to a string in Delphi?
Use IntToStr() for integers and FloatToStr() for floating-point numbers. For more control over formatting (like number of decimal places), use Format('%d', [Value]) or FloatToStrF(). These functions always succeed — conversion from number to string never raises EConvertError.