try throw catch

I'm trying to write a program that prints the second smallest integer in a sequence and outputs an error message if there is no second smalles integer (e.g. in 2 2 2 2)
I got the code to work with valid input. I can't figure out how to implement the try throw and catch. Code is down below, any help will be appreciated :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (int i = 0; i < order.size(); i++) {
        if (order[i] < small) {
        second = small;
        small = order[i];
        } 
        else if (order[i] < second) {
        second = order[i];
        }
        if (order[i] = order[i]) {
            try {
                throw std::runtime_error("error: no second smallest");
            }
            catch (std::runtime_error error) {
                    std::cerr << error.what() << std::endl;
            }
        }
}
1. Doesn't your compiler warn about the assignment on line 9?

2. Even if you had equality comparison, line 9 seems illogical.

3. Why are all of lines 9--16 inside the loop? You cannot possibly know what you have before you have seen it all.

4. If you get into line 10, you will also execute line 14. You could replace the entire 10--15 with a cerr << "error ...";

5. Why have exceptions at all? Your problem does not need any.
Topic archived. No new replies allowed.