Errors

PyMemoryEditor defines a small exception hierarchy. All custom exceptions inherit from PyMemoryEditorError, so you can catch the whole library:

from PyMemoryEditor import PyMemoryEditorError

try:
    with OpenProcess(process_name="game.exe") as process:
        ...
except PyMemoryEditorError as exc:
    print("Library error:", exc)

The hierarchy

Exception
└── PyMemoryEditorError
    β”œβ”€β”€ ClosedProcess
    β”œβ”€β”€ ProcessIDNotExistsError
    β”œβ”€β”€ ProcessNotFoundError
    └── AmbiguousProcessNameError

The library also raises standard built-in exceptions when they are the more idiomatic match (PermissionError, OSError, TypeError, ValueError, NotImplementedError).

Custom exceptions

PyMemoryEditorError

exception PyMemoryEditorError

Base class for every PyMemoryEditor-specific exception. Catch it to handle any library error.

ClosedProcess

exception ClosedProcess

Raised when you call any method on a process whose handle has already been closed (manually or by leaving the with block).

with OpenProcess(pid=1234) as process:
    pass

process.read_process_memory(0x1000, int)   # raises ClosedProcess

ProcessIDNotExistsError

Raised when the given pid= doesn’t correspond to a running process.

exception ProcessIDNotExistsError
pid: int

The PID that was looked up.

ProcessNotFoundError

Raised when no running process matches the given process_name=.

exception ProcessNotFoundError
process_name: str

The process name that was looked up.

AmbiguousProcessNameError

Raised when more than one running process matches the given process_name= (typical when using exact_match=False).

exception AmbiguousProcessNameError
process_name: str

The process name that was looked up.

pids: List[int]

PIDs of the matching processes β€” pick one and pass it via pid=.

Example:

from PyMemoryEditor import OpenProcess, AmbiguousProcessNameError

try:
    OpenProcess(process_name="chrome", exact_match=False)
except AmbiguousProcessNameError as exc:
    print("Multiple matches:", exc.pids)
    process = OpenProcess(pid=exc.pids[0])

Standard exceptions

ExceptionWhen it's raised
TypeErrorNeither process_name nor pid provided to OpenProcess.
ValueErrorInvalid pytype, missing bufflength for str/bytes, invalid ptr_size, malformed pattern, etc.
PermissionErrorOS denied access to the target process or a specific region.
OSErrorLow-level read/write failure (e.g. page was freed between scan and read).
NotImplementedErrorallocate_memory / free_memory on Linux.
UserWarningpermission= passed on a non-Windows platform (silently ignored).
ResourceWarningmacOS: mach_vm_protect failed to restore a page's original protection after a write.

Catching robustly

A pattern that handles every realistic failure mode:

from PyMemoryEditor import (
    OpenProcess,
    PyMemoryEditorError,
    ProcessNotFoundError,
    AmbiguousProcessNameError,
)

try:
    with OpenProcess(process_name="game.exe") as process:
        for address in process.search_by_value(int, value=100):
            try:
                value = process.read_process_memory(address, int)
            except OSError:
                continue   # the page disappeared mid-scan β€” keep going
            print(hex(address), value)

except ProcessNotFoundError:
    print("The game isn't running.")
except AmbiguousProcessNameError as exc:
    print("Pick one PID:", exc.pids)
except PermissionError:
    print("OS denied access β€” see Platform Notes.")
except PyMemoryEditorError as exc:
    print("Library error:", exc)