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.
ProcessNotFoundErrorο
Raised when no running process matches the given process_name=.
AmbiguousProcessNameErrorο
Raised when more than one running process matches the given
process_name= (typical when using exact_match=False).
- exception AmbiguousProcessNameErrorο
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ο
| Exception | When it's raised |
|---|---|
TypeError | Neither process_name nor pid provided to OpenProcess. |
ValueError | Invalid pytype, missing bufflength for str/bytes, invalid ptr_size, malformed pattern, etc. |
PermissionError | OS denied access to the target process or a specific region. |
OSError | Low-level read/write failure (e.g. page was freed between scan and read). |
NotImplementedError | allocate_memory / free_memory on Linux. |
UserWarning | permission= passed on a non-Windows platform (silently ignored). |
ResourceWarning | macOS: 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)
See also