Python Migration Error: TypeError: exceptions must be old-style classes or derived from BaseException, not str

Chris Rebert clp2 at rebertia.com
Mon Oct 3 03:45:34 EDT 2011


On Sun, Oct 2, 2011 at 11:45 PM, Wong Wah Meng-R32813
<r32813 at freescale.com> wrote:
> Hello guys,
>
> I am migrating my application from python 1.5.2 to 2.7.1. I encountered an error when I run some commands (I put in debug statement however, not able to trace down to which line of code that cause it to generate a lot of messages in one second until my hard disk space is full. The error log I got in my log file is as below:-
>
> Oct  3 14:12:41  ('Encountered exception while processing from', (0, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'), <type 'exceptions.TypeError'>, TypeError('exceptions must be old-style classes or derived from BaseException, not str',))
>
> Does it mean in newer python I need to migrate all my Exception to non-string based exception type?

Correct. You can no longer do merely `raise "Some error message"`. You
should instead raise an exception instance of the appropriate type;
e.g. `raise ValueError("foo must be positive")`.
It's advisable to read the NEWS / "What's New" documents for the
intervening versions so you can learn what else has changed.

> That's should be a lot of changes. :p

To be fair, v1.5.2 is practically ancient at this point. It's over a
decade old! And 2 *major* versions behind what's current.
In a pinch, you could always write a script to mechanically change
`raise "..."` to `raise StandardError("...")` [or some other fairly
generic exception type].

Cheers,
Chris



More information about the Python-list mailing list