{"record":{"id":"390b47c5e9e64017","repo":"dotnet/wpf","slug":"sr-quaternion-zeroaxisspecified","errorCode":null,"errorMessage":"SR.Quaternion_ZeroAxisSpecified","messagePattern":"SR\\.Quaternion_ZeroAxisSpecified","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Quaternion.cs","lineNumber":72,"sourceCode":"            _y = y;\n            _z = z;\n            _w = w;\n            _isNotDistinguishedIdentity = true;\n        }\n        \n        /// <summary>\n        /// Constructs a quaternion via specified axis of rotation and an angle.\n        /// Throws an InvalidOperationException if given (0,0,0) as axis vector.\n        /// </summary>\n        /// <param name=\"axisOfRotation\">Vector representing axis of rotation.</param>\n        /// <param name=\"angleInDegrees\">Angle to turn around the given axis (in degrees).</param>\n        public Quaternion(Vector3D axisOfRotation, double angleInDegrees)\n        {\n            angleInDegrees %= 360.0; // Doing the modulo before converting to radians reduces total error\n            double angleInRadians = angleInDegrees * (Math.PI / 180.0);\n            double length = axisOfRotation.Length;\n            if (length == 0)\n                throw new System.InvalidOperationException(SR.Quaternion_ZeroAxisSpecified);\n            Vector3D v = (axisOfRotation / length) * Math.Sin(0.5 * angleInRadians);\n            _x = v.X;\n            _y = v.Y;\n            _z = v.Z;\n            _w = Math.Cos(0.5 * angleInRadians);\n            _isNotDistinguishedIdentity = true;\n        }\n\n        #endregion Constructors\n\n        \n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n\n        #region Public Methods","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Quaternion.cs#L54-L90","documentation":"The Quaternion(Vector3D axisOfRotation, double angleInDegrees) constructor builds a rotation quaternion from an axis-angle pair. If the axis vector has length 0 (Vector3D.Zero), there is no rotation axis, so the constructor throws InvalidOperationException with Quaternion_ZeroAxisSpecified.","triggerScenarios":"new Quaternion(new Vector3D(0,0,0), angle) or any axis vector whose X/Y/Z are all 0; also axes that degenerate to zero through computed expressions (e.g. cross product of parallel vectors) before being passed to the constructor.","commonSituations":"Computing an axis from a cross product of parallel vectors (result is zero); uninitialized Vector3D fields defaulting to (0,0,0); config/data producing a zero direction vector for rotations.","solutions":["Before constructing, check axisOfRotation.Length != 0 and supply a valid non-zero axis.","If the axis is computed (e.g. from a cross product), handle the parallel/zero case explicitly and fall back to a default axis such as (0,1,0).","If no rotation is desired, use the identity Quaternion (Quaternion.Identity) instead of constructing with a zero axis.","Wrap the constructor in try/catch for InvalidOperationException when axis data comes from external input."],"exampleFix":"// before\nvar q = new Quaternion(axis, angle); // throws if axis == (0,0,0)\n// after\nvar q = axis.Length == 0 ? Quaternion.Identity : new Quaternion(axis, angle);","handlingStrategy":"validation","validationCode":"if (axisOfRotation.Length == 0) { /* use Quaternion.Identity or fix axis */ }","typeGuard":"static bool IsValidRotationAxis(Vector3D axis) => axis.Length > 0;","tryCatchPattern":"try { var q = new Quaternion(axis, angleDegrees); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"axis\"))\n{\n    var q = Quaternion.Identity; // degenerate axis: no rotation\n}","preventionTips":["Always check vector length before axis-angle construction","Handle zero results from cross-product-derived axes (parallel vectors)","Normalize computed axes before use","Treat zero axis as 'no rotation' and use Quaternion.Identity"],"tags":["wpf","media3d","quaternion","zero-vector"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}