{"record":{"id":"de8a5eb3d74dfc53","repo":"TheAlgorithms/C-Sharp","slug":"vertex-already-exists-currentedgeweight","errorCode":null,"errorMessage":"Vertex already exists: {currentEdgeWeight}","messagePattern":"Vertex already exists: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Graph/DirectedWeightedGraph.cs","lineNumber":214,"sourceCode":"        if (capacity < 0)\n        {\n            throw new InvalidOperationException(\"Graph capacity should always be a non-negative integer.\");\n        }\n    }\n\n    private static void ThrowIfWeightZero(double weight)\n    {\n        if (weight.Equals(0.0d))\n        {\n            throw new InvalidOperationException(\"Edge weight cannot be zero.\");\n        }\n    }\n\n    private static void ThrowIfEdgeExists(double currentEdgeWeight)\n    {\n        if (!currentEdgeWeight.Equals(0.0d))\n        {\n            throw new InvalidOperationException($\"Vertex already exists: {currentEdgeWeight}\");\n        }\n    }\n\n    private void ThrowIfOverflow()\n    {\n        if (Count == capacity)\n        {\n            throw new InvalidOperationException(\"Graph overflow.\");\n        }\n    }\n\n    private void ThrowIfVertexNotInGraph(Vertex<T> vertex)\n    {\n        if (vertex.Graph != this)\n        {\n            throw new InvalidOperationException($\"Vertex does not belong to graph: {vertex}.\");\n        }\n    }","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Graph/DirectedWeightedGraph.cs#L196-L232","documentation":"ThrowIfEdgeExists throws when AddEdge is called for a vertex pair that already has an edge. The (misleading) message interpolates the existing edge's weight, e.g. 'Vertex already exists: 5'. The library does not silently overwrite edges; callers must remove the edge first.","triggerScenarios":"Calling AddEdge(u, v, w) twice for the same vertex pair; the second call finds the current matrix weight non-zero and throws.","commonSituations":"Re-running graph-building code without clearing state, importing edge lists containing duplicate edges, or trying to update an edge's weight via AddEdge instead of RemoveEdge-then-AddEdge.","solutions":["Check first: if (!AreAdjacent(u, v)) AddEdge(u, v, w); else RemoveEdge then AddEdge.","Deduplicate edge input before insertion (distinct on the vertex pair).","Wrap in try/catch for InvalidOperationException when re-adding is expected/ignorable."],"exampleFix":"// before\ngraph.AddEdge(a, b, 2.0);\ngraph.AddEdge(a, b, 3.0); // throws\n// after\nif (graph.AreAdjacent(a, b)) graph.RemoveEdge(a, b);\ngraph.AddEdge(a, b, 3.0);","handlingStrategy":"validation","validationCode":"if (!graph.AreAdjacent(u, v)) { graph.AddEdge(u, v, w); }","typeGuard":null,"tryCatchPattern":"try { graph.AddEdge(u, v, w); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Vertex already exists:\")) { /* edge already present: ignore or update */ }","preventionTips":["Check AreAdjacent before adding.","Deduplicate edge lists on the (from,to) pair before import.","Use RemoveEdge + AddEdge to change an existing weight."],"tags":["csharp","graph","duplicate-edge","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}