Standard_DivideByZero in Open CASCADE Technology

Diagnostic signature

Standard_DivideByZero

Standard_DivideByZero is an OCCT exception signaling an attempt to divide by zero. It is explicitly thrown by geometric routines or emitted via hardware traps to prevent undefined behavior.

What it means

In Open CASCADE Technology (OCCT), `Standard_DivideByZero` is a diagnostic C++ exception class inheriting from `Standard_NumericError` and `Standard_Failure`. It signals that a mathematical or geometric operation attempted to divide a numerical value by zero. This diagnostic is designed to halt the current operation and prevent the silent propagation of Not-a-Number (NaN) or infinity (INF) values through the geometry kernel. Unchecked infinities would otherwise cause silent topology corruption, malformed boundary representations, and non-deterministic behavior in downstream CAD algorithms.

Why it happens

This diagnostic is triggered in one of two main ways within an OCCT toolchain: 1. Explicit Validations: OCCT's numerical and geometric algorithms explicitly check denominators before executing division. If a denominator evaluates to exactly zero (or occasionally falls below a specific tolerance), the code executes the `Standard_DivideByZero_Raise_if` macro or natively throws the exception. This frequently occurs when normalizing zero-length vectors, projecting singular points, or evaluating curve derivatives at strict cusps. 2. Hardware Signal Trapping: If the application configures hardware exception handling via `OSD::SetSignal()` and wraps the execution flow in the `OCC_CATCH_SIGNALS` macro, operating system-level floating-point exceptions (SIGFPE) are intercepted. OCCT's error handler captures the C-style signal and natively re-raises it as a `Standard_DivideByZero` C++ exception, enforcing safe unwinding.

Minimal reproduction

#include <Standard_DivideByZero.hxx>

double CalculateScaleFactor(double targetLength, double currentLength) {
    // Explicitly triggers the diagnostic if currentLength is exactly 0.0
    Standard_DivideByZero_Raise_if(currentLength == 0.0, "currentLength cannot be zero");
    return targetLength / currentLength;
}

int main() {
    // Triggers Standard_DivideByZero
    double factor = CalculateScaleFactor(10.0, 0.0);
    return 0;
}

How to fix it

Validate geometric and numeric denominators against a standardized acceptable tolerance threshold (such as `Precision::Confusion()`) before performing division, implementing a fallback strategy for singularities.

```C++
#include <Standard_DivideByZero.hxx>
#include <Precision.hxx>
#include <cmath>
#include <iostream>

double CalculateScaleFactor(double targetLength, double currentLength) {
    // Validate against OCCT's mathematical confusion tolerance instead of absolute zero
    if (std::abs(currentLength) <= Precision::Confusion()) {
        std::cerr << "Warning: Singular length detected. Defaulting scale to 1.0." << std::endl;
        return 1.0; // Fallback to avoid division by near-zero values
    }
    return targetLength / currentLength;
}

int main() {
    // Safely executes and outputs the fallback value without raising the exception
    double factor = CalculateScaleFactor(10.0, 0.0);
    std::cout << "Calculated scale factor: " << factor << std::endl;
    return 0;
}
```

Step 1

Step 2

Step 3

Step 4

Upstream references

Open CASCADE Technology OCCT 7.8.0 — Standard_DivideByZero.hxx — retrieved 2026-08-11

Standard_ErrorHandler.hxx Source Declaration (OCCT 7.8.0)