Skip to content

ValueError: invalid literal for int() with base 10: โ€‹

handling ValueError raised by wrong type conversion in Python

python
try:
    value = int("invalid")
except ValueError:
    print("ValueError: invalid literal for int() with base 10")

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

In this code snippet, the variable "value" is being set to the integer representation of the string "invalid." However, "invalid" is not a valid integer, so the int() function will raise a ValueError. The except block catches this error and prints the error message "ValueError: invalid literal for int() with base 10."

Do you find this helpful?

Dual-run preview โ€” compare with live Symfony routes.