---
title: "Square root with exception"
subject: "Object Oriented Programming"
chapter: 10
key: "semester_3/object_oriented_programming:sqrt-exception-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/sqrt-exception-program
site: BCT Engineering
---

# Square root with exception

Object Oriented Programming · Chapter 10: Exception Handling

- Asked in: 2072 Chaitra, 5 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/sqrt-exception-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-10

## Question

Write a program to find the square root of given number. Check the validity of input number and raise the exception as per requirement.

## Short answer

Define a class NegativeNumber to carry the invalid value. In findSqrt() throw NegativeNumber(x) if x<0. In main(), catch NegativeNumber and print error; also check for non-numeric input with cin.

## Answer

Validity rule: the square root of a negative number is not real, so a negative input raises an exception. Non-numeric input is also checked.

```cpp
#include <iostream>
#include <cmath>
using namespace std;

class NegativeNumber {
public:
    double value;
    NegativeNumber(double v) : value(v) {}
};

double findSqrt(double x) {
    if (x < 0)
        throw NegativeNumber(x);       // raise exception
    return sqrt(x);
}

int main() {
    double n;
    cout << "Enter a number: ";
    if (!(cin >> n)) {
        cout << "Invalid input: not a number\n";
        return 1;
    }
    try {
        cout << "Square root = " << findSqrt(n) << endl;
    }
    catch (NegativeNumber e) {
        cout << "Error: cannot find square root of negative number "
             << e.value << endl;
    }
    return 0;
}
```

**Sample runs**

```text
Enter a number: 49
Square root = 7

Enter a number: -9
Error: cannot find square root of negative number -9
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When does findSqrt throw an exception?
   A. When x is zero
   B. When x is greater than 100
   C. When x is negative
   D. When x is an integer
   Answer: C. The square root of a negative number is not real. (Key point: Validity condition)

2. How does the program deal with non-numeric input?
   A. Throws NegativeNumber
   B. Ignores it
   C. Uses eof()
   D. Tests if(!(cin >> n)) and prints an error message
   Answer: D. The failed extraction is detected from the stream state. (Key point: Non-numeric input)

3. Why does NegativeNumber store the value?
   A. So the handler can report the offending number
   B. To compute sqrt
   C. To avoid a catch
   D. To read input
   Answer: A. The exception carries the value so the handler prints it in the message. (Key point: Exception object carries data)

## Related in Object Oriented Programming

- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6

## Related in other subjects

- [Square root circuit and √((V1V2)²+(V3V4)²)](https://www.bctengineer.com/question.md?key=semester_4%2Fadvanced_electronics%3Asquare-root-vector-magnitude) — Advanced Electronics, chapter 6, asked 2×
- [Root-locus design: 8 samples per cycle, T = 0.2 s](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Aroot-locus-8-samples-t02) — Digital Control System, chapter 4
- [Check triangular number](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Atriangular-number) — Computer Programming, chapter 5
- [Square root of time method for cv](https://www.bctengineer.com/question.md?key=semester_4%2Fsoil_mechanics%3Asqrt-time-method-cv) — Soil Mechanics, chapter 9
