Fwd: Newbie naive question ... int() throws ValueError

Karl Knechtel zahlman at gmail.com
Sat May 12 08:27:14 EDT 2012


I really wish gmail picked up the mailing list as a default reply-to address...


---------- Forwarded message ----------
From: Karl Knechtel <zahlman at gmail.com>
Date: Sat, May 12, 2012 at 8:25 AM
Subject: Re: Newbie naive question ... int() throws ValueError
To: Devin Jeanpierre <jeanpierreda at gmail.com>


On Sat, May 12, 2012 at 12:11 AM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
> I'm not talking about unexpected exceptions. I'm saying, if I expect
> invalid input for int, where should I go to find out how to deal with
> said invalid input properly? How do I know that int raises ValueError
> on failure, and not, for example, something like ArgumentError
> (something that Python doesn't have) or (like chr) TypeError?
>
> Apparently the answer is to read the documentation for ValueError.

The easiest way is to try it. In fact, in most cases, this will be
easier than looking it up in the documentation could ever be, because
looking something up in documentation requires you to read through a
bunch of documentation until you find key info like 'raises FooError
in bar circumstance', and then interpret it; by trial and error, you
see exactly what happens right away, and you can do it by just banging
out a couple of lines on the REPL.

You should have already read the documentation for things like
ValueError as a part of learning the language (i.e. the first time
something raised a ValueError and you wanted to know what that meant);
or at the very least you should have a good idea of what the
"standard" exceptions are all named, and *develop an intuition* for
which apply to what circumstances. As noted by others, you should not
expect `int` to throw a `TypeError` when fed a bad string, because a
`TypeError` means "something is wrong with the type of some object",
and the problem with the bad string isn't that it's a string (its
type), but that the string contents are not interpretable as int (its
value, hence ValueError).

--
~Zahlman {:>


-- 
~Zahlman {:>



More information about the Python-list mailing list