exception guidelines for launchpad - good idea?
Martin Pool
mbp at sourcefrog.net
Thu Dec 8 05:26:47 GMT 2005
On 8 Dec 2005, Robert Collins <robertc at robertcollins.net> wrote:
> > It looks okay, but the stuff on TypeError isn't clear to me. I think
> > it's quite reasonable to throw TypeError if we get an argument of the
> > wrong type. And since we are writing a library, the rationale about
> > writing an app doesn't hold.
I agree, both that in general they make sense and that it does seem OK
to throw TypeError, but only to catch it in specific cases.
One thing that seems to be missing is that you should never catch all
exceptions without re-raising them unless there is an *extremely* good
justification.
> The point about not writing a general purpose library is that its better
> to die than to swallow a bunch of random exceptions and have bugs
> undiagnosed. In bzr's case the same applies - we dont want bugs to sit
> around for long periods of time because they are caught by erroneously
> robust code.
Sure. I don't think it's a good idea for libraries to hide exceptions
either, for that matter.
> The thing about exceptions is who will catch them and why.
>
> Firstly, do you see why catching TypeError is bad? Catching TypeError
> is bad because (due to python shortcomings) using the wrong number of
> parameters - plain broken code - is conflated with some constructor
> errors in built in types. This makes it possible to catch exceptions you
> dont want to (because broken code should be exposed).
I agree that it is a bad idea to catch TypeError except in very
tightly-defined cases - e.g. when trying to convert a string to an int,
as in the doc.
> Building on that, if we are not catching TypeError anywhere because of
> this confusion, we *could* raise TypeError when we wanted a unicode
> string but got a normal string, but we would be furthering that
> confusion.
I don't see how that follows. I'd say it's OK to raise TypeError or
ValueError if you got the wrong type AND you don't expect callers to try
to catch this, but rather that it will just indicate a bug. There's no
point subclassing TypeError.
What are you supposed to raise if you get the wrong type?
--
Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20051208/b3878e93/attachment.pgp
More information about the bazaar
mailing list