[Tutor] Inherit from SyntaxError?

Albert-Jan Roskam sjeik_appie at hotmail.com
Thu Mar 23 04:17:03 EDT 2023


   On Mar 21, 2023 22:11, Cameron Simpson <cs at cskk.id.au> wrote:

     On 21Mar2023 18:07, Albert-Jan Roskam <sjeik_appie at hotmail.com> wrote:
     >   Thanks! I now created one BaseParserError (inherits from
     >   SyntaxError)
     >   and a lexer + parser error (they each inherit from BaseParserError).

     Sounds good.

     >   So, is MemoryError the only really tricky exception? I mean, the
     >   interpreter might just die before it is able to recover from the
     >   MemoryError?

     You probably can't do anything about this. What kind of recovery would
     you do? And why?

     Remember the rule: only catch things you have a meaningful action to
     catch (there are reare exceptions, usually around daemon like activity
     where you're running some task: catch and log the exception, and poceeed
     to the next task/request - you're not "recovering" or repairing here,
     just not terminating some outer framework).

     What's tricky about a MemoryException? Just let it out, like a bunch of
     other "system" type exceptions. Can you describe what you want to do
     with MemoryError?

   ====
   Hi Cameron,
   Sometimes catching MemoryError might come in handy with a "inside/outside
   RAM" strategy. In-memory is faster, but not scalable.
   For instance, something like:
   try:
       d = {}
       for record in records:
           d[record.id] = record.something
   except MemoryError:
       logger.warning("Using slower shelve route")
       d = shelve.open("/tmp/lookup.shelve", writeback=True)
       atexit.register(d.close)
       for record in records:
           d[record.id] = record.something
   If you know that "records" is always huge, this approach is needlessly
   complicated. But if it *might* be too big for some machines, it might be
   nice not having to choose the slow approach, just to be on the safe side.
   Best wishes,
   Albert-Jan


More information about the Tutor mailing list