Exception and tools

Exception and tools

  • Expecting basics: Why use exceptions?; The short history.

Exception processing is good for: error handling, termination action and event notification. How can your script recover from an exception: try and except statements to catch and recover from exceptions. Trigger exceptions in your script: raise and assert statements. Specify actions to be run at termination time: try, finally, with and as statements.

  • Exception coding details: The try/except/else statement; The try/finally statement; Unified try/except/finally; the raise statement; The assert statement; With/As context managers.

try statement: catches and recovers from exceptions. common variations of the try statement: try/except/else for catching exceptions and try/finally for specifying cleanup actions. raise statement: trigger an exception. assert statement: assertionError exception. with/as statement: automate starup and termination activities.

  • Exception objects: Back to the future; Why exceptions hierarchies?; Built-in exception classes; Custom print display; Custom data and behavior.

Constrains on user-defined exceptions: Exceptions must be defined by classes BaseException Match handlers and class-based exceptions: by superclass relationships. Think in superclasses as general exception categories and subclasses as more specific types of exceptions within categories. Attach context information to exception objects: filling out instance attributes in the instance object raised (custom class constructor). Specify the error message text for exception objects: custom str operator overloading method (or print and str automatically). Why should you not use string-based exceptions: not support categories, state information, behaviour inheritance in the way class-based exceptions do.

  • Designing with exceptions: Nesting exception handlers; Exception idioms; Exception design tips and gotchas.

Última actualización

¿Te fue útil?