{"record":{"id":"587cb3edb3911a79","repo":"BoundaryML/baml","slug":"baml-panics-divisionbyzero","errorCode":"baml.panics.DivisionByZero","errorMessage":"division by zero: {left:?} / {right:?}","messagePattern":"division by zero: (.+?) / (.+?)","errorType":"error_code","errorClass":"VmPanic","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":24,"sourceCode":"//! these types to heap-allocated exception `Value`s\n//! (`panic_to_exception_value` / `error_to_exception_value`) stays in\n//! `bex_vm` because it needs VM state (heap, class table).\n\nuse thiserror::Error;\n\nuse crate::{\n    BinOp, CmpOp, SysOpErrorCategory, UnaryOp, Value,\n    types::{ObjectType, Type},\n};\n\n/// A catchable BAML panic — maps 1:1 to a `baml.panics.*` class.\n///\n/// These are user-visible runtime errors (division by zero, index out of\n/// bounds, etc.) that can be caught by `catch` handlers. The handler's\n/// `ThrowIfPanic` instruction filters which panics are caught vs rethrown.\n#[derive(Debug, Error, PartialEq, Clone)]\npub enum VmPanic {\n    #[error(\"division by zero: {left:?} / {right:?}\")]\n    DivisionByZero { left: Value, right: Value },\n\n    /// An `int` (i63) arithmetic operation overflowed the representable\n    /// range `[INT_MIN, INT_MAX]`. Carries a human-readable description of\n    /// the operation (e.g. `\"4611686018427387903 + 1\"`); built only on the\n    /// cold overflow path, so the `String` alloc never touches hot code.\n    #[error(\"integer overflow: {message}\")]\n    IntegerOverflow { message: String },\n\n    // Raised by array and byte-array subscripting, so the message stays generic\n    // (\"index\", not \"array index\").\n    #[error(\"index out of bounds: {index} of {length}\")]\n    IndexOutOfBounds { index: i64, length: usize },\n\n    #[error(\"invalid field access: field {field_index} of {field_count}\")]\n    InvalidFieldAccess {\n        field_index: usize,\n        field_count: usize,","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L6-L42","documentation":"`VmPanic::DivisionByZero` is a user-visible VM runtime panic raised when an integer division (or modulo-style operation) is performed with a zero divisor. Unlike internal panics, these are part of the language's error model: they carry the operand `Value`s for a readable message and can be intercepted by user `catch` handlers — the `ThrowIfPanic` instruction filters which panics a handler catches versus rethrows.","triggerScenarios":"Executing a BAML division instruction `left / right` where `right` evaluates to an int value of 0; e.g. `x / y` where y comes from user input, an empty-collection count, or a computed denominator that reaches 0 at runtime.","commonSituations":"Computing averages/percentages where a collection is empty; user-supplied numeric input of 0; off-by-one logic producing a zero denominator; unguarded config-derived values used as divisors.","solutions":["Guard the divisor before dividing: branch on `right == 0` and return a default/error value instead of dividing.","Wrap the division in a `catch` handler in BAML so `ThrowIfPanic` catches DivisionByZero and your handler recovers.","Validate user/config-derived inputs at the boundary, rejecting zero where a denominator is required.","Log the operands (they're included in the panic) to find where the zero value originates."],"exampleFix":"// before (BAML)\nlet ratio = total / count;\n// after\nlet ratio = if count == 0 { 0 } else { total / count };","handlingStrategy":"try-catch","validationCode":"// check the divisor before dividing (host-side guard)\nif right.as_int() == Some(0) { return Err(\"denominator must be non-zero\"); }","typeGuard":null,"tryCatchPattern":"// in BAML: catch the panic and recover\ncatch (e) {\n  // ThrowIfPanic matches VmPanic::DivisionByZero here\n  fallback_value()\n} { ratio = total / count; }","preventionTips":["Always branch on the divisor being 0 before dividing, especially for averages and percentages.","Validate numeric user/config inputs at the boundary and reject zero denominators.","Use catch handlers for computed divisions whose operands you cannot fully control.","Log operand values from the panic message to trace where zero originated."],"tags":["rust","vm","runtime-error","arithmetic","baml"],"backgroundTag":"division-by-zero","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}