Built-in Exceptions

OSError — exception class

Base for operating-system related failures. Constructing OSError with a recognized errno may select a more specific subclass; error details can include errno, strerror, filename and filename2. Windows-specific fields and error mappings vary. Use narrower subclasses for expected failures. The example raises a controlled stand-in to demonstrate handling; it does not cause the real system failure. Catch OSError when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
OSError

Return Value

type

The class object OSError; this reference documents the class identity, not a universal constructor signature.

Working Example

Example Snippetpython
import errnotry:    raise OSError(errno.ENOENT,"missing","demo.txt")except OSError as error:    print(type(error).__name__)
Python Reference