{"record":{"id":"208b4016c674d07a","repo":"BoundaryML/baml","slug":"thrown-value","errorCode":null,"errorMessage":"thrown value","messagePattern":"thrown value","errorType":"exception","errorClass":"VmRustFnError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":477,"sourceCode":"}\n\n/// An error returned by a Rust function. Will generally be turned into a [`VmError`].\n/// This is separate from [`VmError`] so native Rust functions can return standard errors\n/// without needing to handle heap allocation.\n#[derive(Debug, Error, PartialEq, Clone)]\npub enum VmRustFnError {\n    #[error(\"{0}\")]\n    Panic(#[from] VmPanic),\n    #[error(\"{0}\")]\n    BamlError(#[from] VmBamlError),\n    #[error(\"{0}\")]\n    InternalError(#[from] VmInternalError),\n    /// A pre-built exception `Value` to throw directly as a catchable error.\n    ///\n    /// Used by native functions that need to throw user-defined class instances\n    /// (e.g. `baml.json.ParseError`) without going through the\n    /// `VmPanic` / `VmBamlError` enumeration machinery.\n    #[error(\"thrown value\")]\n    Thrown {\n        value: Value,\n        profiler_kind: ProfilerErrorKind,\n    },\n}\n\nimpl VmRustFnError {\n    #[must_use]\n    pub const fn thrown_fresh(value: Value) -> Self {\n        Self::Thrown {\n            value,\n            profiler_kind: ProfilerErrorKind::Fresh,\n        }\n    }\n\n    #[must_use]\n    pub const fn thrown_rethrow(value: Value) -> Self {\n        Self::Thrown {","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L459-L495","documentation":"This error variant (VmError::Thrown { value, profiler_kind }) displays as \"thrown value\" and represents a pre-built exception Value to be thrown directly as a catchable error. Native functions use it to throw user-defined class instances such as baml.json.ParseError without going through the VmPanic/VmBamlError enumeration machinery.","triggerScenarios":"A native function (e.g. JSON parsing) needs to throw a user-defined exception instance; the VM raises it as a catchable error value; when uncaught it surfaces with the generic message \"thrown value\".","commonSituations":"Parsing malformed JSON in BAML and baml.json.ParseError escaping without a catch handler; host-defined exception classes thrown by native helpers; the generic message hides the real cause until the thrown Value is inspected.","solutions":["Catch the thrown value and inspect it — it is a real class instance (e.g. baml.json.ParseError) with useful fields.","Validate/normalize JSON input before calling parsing functions so the throw never happens.","Note the message is generic by design (\"thrown value\"); enable profiler-kind/error logging for more context.","Handle the specific exception class name in your catch logic."],"exampleFix":"// before: raw parse throws and escapes\nlet data = baml.json.Parse(input)\n\n// after: guard the input first\nlet data = input.trim().len() > 0 && is_valid_json(input)\n  ? baml.json.Parse(input)\n  : null","handlingStrategy":"validation","validationCode":"// Check JSON validity before invoking the parsing native function\nfn looks_like_json(s: &str) -> bool {\n    let t = s.trim();\n    !t.is_empty()\n        && ((t.starts_with('{') && t.ends_with('}'))\n            || (t.starts_with('[') && t.ends_with(']'))\n            || (t.starts_with('\"') && t.ends_with('\"')))\n}","typeGuard":null,"tryCatchPattern":"match vm_result {\n    Err(BexError::Thrown { value, .. }) => {\n        if let Some(pe) = value.as_instance_of(\"baml.json.ParseError\") {\n            eprintln!(\"JSON parse error: {:?}\", pe);\n        }\n    }\n    other => other?,\n}","preventionTips":["Validate/normalize JSON input before parsing.","Catch and inspect the thrown class instance rather than relying on the generic message.","Enable profiler/error-kind logging to see which native function threw.","Return nullable results from parse helpers instead of letting throws escape."],"tags":["baml","vm","json","thrown-value"],"backgroundTag":"json-parse-error","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}