{"record":{"id":"35a03278a24e4bf2","repo":"TheAlgorithms/Python","slug":"principal-borrowed-must-be-0","errorCode":null,"errorMessage":"Principal borrowed must be > 0","messagePattern":"Principal borrowed must be > 0","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"financial/equated_monthly_installments.py","lineNumber":38,"sourceCode":"    >>> equated_monthly_installments(25000, 0.12, 3)\n    830.3577453212793\n    >>> equated_monthly_installments(25000, 0.12, 10)\n    358.67737100646826\n    >>> equated_monthly_installments(0, 0.12, 3)\n    Traceback (most recent call last):\n        ...\n    Exception: Principal borrowed must be > 0\n    >>> equated_monthly_installments(25000, -1, 3)\n    Traceback (most recent call last):\n        ...\n    Exception: Rate of interest must be >= 0\n    >>> equated_monthly_installments(25000, 0.12, 0)\n    Traceback (most recent call last):\n        ...\n    Exception: Years to repay must be an integer > 0\n    \"\"\"\n    if principal <= 0:\n        raise Exception(\"Principal borrowed must be > 0\")\n    if rate_per_annum < 0:\n        raise Exception(\"Rate of interest must be >= 0\")\n    if years_to_repay <= 0 or not isinstance(years_to_repay, int):\n        raise Exception(\"Years to repay must be an integer > 0\")\n\n    # Yearly rate is divided by 12 to get monthly rate\n    rate_per_month = rate_per_annum / 12\n\n    # Years to repay is multiplied by 12 to get number of payments as payment is monthly\n    number_of_payments = years_to_repay * 12\n\n    return (\n        principal\n        * rate_per_month\n        * (1 + rate_per_month) ** number_of_payments\n        / ((1 + rate_per_month) ** number_of_payments - 1)\n    )\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/financial/equated_monthly_installments.py#L20-L56","documentation":"Raised by equated_monthly_installments() in financial/equated_monthly_installments.py when principal <= 0. The EMI formula multiplies principal into the annuity factor, so a non-positive loan amount is meaningless; this is the first of three sequential input checks (principal, then rate, then years). Note the function raises bare Exception, not ValueError — so a `except ValueError` handler will not catch it.","triggerScenarios":"equated_monthly_installments(-25000, 0.12, 3) or principal=0; amounts parsed from user input where an empty string became 0; loans denominated in subunits (paise/cents) accidentally divided down to 0.","commonSituations":"Form fields defaulting to 0; currency conversions that floor small values to 0; test fixtures using 0 as a 'no loan' sentinel.","solutions":["Pass a strictly positive principal in the loan's currency units.","Validate at the input boundary (form/parser): require principal > 0 before calling.","When catching, use `except Exception` (or a broad catch) because this library raises bare Exception, unlike its ValueError-based siblings."],"exampleFix":"# before\nemi = equated_monthly_installments(0, 0.12, 3)  # Exception\n\n# after\nif principal > 0:\n    emi = equated_monthly_installments(principal, 0.12, 3)","handlingStrategy":"validation","validationCode":"if principal <= 0:\n    raise LoanInputError(\"principal must be > 0\")","typeGuard":"def valid_principal(p) -> bool:\n    return isinstance(p, (int, float)) and p > 0","tryCatchPattern":"# NOTE: this function raises bare Exception, not ValueError\ntry:\n    emi = equated_monthly_installments(principal, rate, years)\nexcept Exception as exc:  # intentional: matches the library's bare raise\n    raise LoanInputError(str(exc)) from exc","preventionTips":["Require principal > 0 in form validation before calling.","Remember this module raises Exception (not ValueError) — write your handler accordingly.","Reject zero-defaulted optional loan fields at deserialization."],"tags":["finance","emi","validation","bare-exception"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}