Try-except for flow control in reading Sqlite

Chris Angelico rosuav at gmail.com
Mon Oct 28 02:36:05 EDT 2013


On Mon, Oct 28, 2013 at 2:43 PM, Victor Hooi <victorhooi at gmail.com> wrote:
> Is it acceptable to use try-except in order to achieve this? E.g.:
>
>     try:
>         # Try to open up the SQLite file, and lookup the required entries
>     except OSError:
>         # Open an empty SQLite file, and create the schema
>
>
> My thinking is that it is (easier to ask forgiveness than permission), but I just wanted to check if there is a better way of achieving this?

That looks fine as a model, but is OSError what you want to be
catching? I'd go with FileNotFoundError if that's what you're looking
for - OSError would also catch quite a bit else, like permissions
errors.

ChrisA



More information about the Python-list mailing list