API reference
Errors
Native errors never reach JS as raw C++ exceptions. Every failure is an instance of np.NativpyError, with a stable code string and a NumPy-style message. Subclasses: ShapeError, DTypeError, IndexError, BroadcastError, ValueError, MemoryError, NotImplementedError, LinAlgError and FloatingPointError.
| Name | Summary |
|---|---|
np.NativpyError | Catch a specific subclass, or NativpyError for any library error. |
np.LinAlgError | Common failures and the error types they raise. |
np.FloatingPointError | Raised for a floating-point error whose np.seterr mode is "raise". |
np.NativpyError
#err instanceof np.ShapeError · err.code
Catch a specific subclass, or NativpyError for any library error.
Returns
—
Example
TypeScript
try {
np.zeros(2).reshape(3);
} catch (err) {
err instanceof np.ShapeError; // => true
err instanceof np.NativpyError; // => true
err.code; // => "NATIVPY_SHAPE_ERROR"
err.message; // => "cannot reshape array of size 2 into shape (3,)"
}np.LinAlgError
#np.LinAlgError · np.BroadcastError · np.IndexError
Common failures and the error types they raise.
Returns
—
Example
TypeScript
const code = (f) => { try { f(); } catch (e) { return e.name; } };
code(() => np.linalg.inv([[1, 2], [2, 4]])); // => "LinAlgError"
code(() => np.add([1, 2], [1, 2, 3])); // => "BroadcastError"
code(() => np.array([1, 2]).get(5)); // => "IndexError"np.FloatingPointError
#np.FloatingPointError
Raised for a floating-point error whose np.seterr mode is "raise".
Returns
—
Example
TypeScript
const code = (f) => { try { f(); } catch (e) { return e.name; } };
np.errstate({ divide: "raise" }, () => code(() => np.divide([1], [0]))); // => "FloatingPointError"