Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime OrtException

From Leeroopedia
Revision as of 10:46, 27 September 2026 by Agent (talk | contribs) (Sync from local file)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources Description
Source File java/src/main/java/ai/onnxruntime/OrtException.java
Repository Microsoft/onnxruntime

Domains

  • Machine Learning Runtime
  • Error Handling

Overview

OrtException is a checked exception containing the error message and error code produced by the native ONNX Runtime or by Java-side validation. It extends Exception and includes the inner enum OrtErrorCode which maps all native error codes (ORT_OK through ORT_NOT_FOUND) plus a Java-specific ORT_JAVA_UNKNOWN sentinel value.

Description

The class provides:

  • Constructors: Three variants accepting (String), (int, String), or (OrtErrorCode, String). The int-based constructor is used by native JNI code.
  • Error code access: getCode() returns the OrtErrorCode.

The OrtErrorCode enum maps the native OrtErrorCode struct from onnxruntime_c_api.h:

  • ORT_JAVA_UNKNOWN (-1), ORT_OK (0), ORT_FAIL (1), ORT_INVALID_ARGUMENT (2), ORT_NO_SUCHFILE (3), ORT_NO_MODEL (4), ORT_ENGINE_ERROR (5), ORT_RUNTIME_EXCEPTION (6), ORT_INVALID_PROTOBUF (7), ORT_MODEL_LOADED (8), ORT_NOT_IMPLEMENTED (9), ORT_INVALID_GRAPH (10), ORT_EP_FAIL (11), ORT_MODEL_LOAD_CANCELED (12), ORT_MODEL_REQUIRES_COMPILATION (13), ORT_NOT_FOUND (14).

Code Reference

Source Location

// File: java/src/main/java/ai/onnxruntime/OrtException.java
// Package: ai.onnxruntime

Signature

public class OrtException extends Exception {
    public OrtException(String message);
    public OrtException(int code, String message);
    public OrtException(OrtErrorCode code, String message);
    public OrtErrorCode getCode();

    public enum OrtErrorCode {
        ORT_JAVA_UNKNOWN(-1), ORT_OK(0), ORT_FAIL(1),
        ORT_INVALID_ARGUMENT(2), ORT_NO_SUCHFILE(3), ORT_NO_MODEL(4),
        ORT_ENGINE_ERROR(5), ORT_RUNTIME_EXCEPTION(6), ORT_INVALID_PROTOBUF(7),
        ORT_MODEL_LOADED(8), ORT_NOT_IMPLEMENTED(9), ORT_INVALID_GRAPH(10),
        ORT_EP_FAIL(11), ORT_MODEL_LOAD_CANCELED(12),
        ORT_MODEL_REQUIRES_COMPILATION(13), ORT_NOT_FOUND(14);

        public static OrtErrorCode mapFromInt(int value);
    }
}

Import

import ai.onnxruntime.OrtException;
import ai.onnxruntime.OrtException.OrtErrorCode;

I/O Contract

Inputs

Name Type Description
code int or OrtErrorCode The native error code
message String The error description

Outputs

Name Type Description
getCode() OrtErrorCode The structured error code
getMessage() String The formatted error message (inherited from Exception)

Usage Examples

import ai.onnxruntime.*;

OrtEnvironment env = OrtEnvironment.getEnvironment();
try {
    OrtSession session = env.createSession("/nonexistent/model.onnx");
} catch (OrtException e) {
    System.err.println("Error code: " + e.getCode());     // e.g., ORT_NO_SUCHFILE
    System.err.println("Message: " + e.getMessage());

    if (e.getCode() == OrtException.OrtErrorCode.ORT_NO_SUCHFILE) {
        System.err.println("Model file not found.");
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment