Finding errors in code about exceptions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
enum Ball {FASTBALL, CURVEBALL, SLIDER, CHANGEUP};

enum Punch {UPPERCUT, HOOK, JAB};

int MAX_LIFE = 20;

// MODIFIES: life
// EFFECTS: If life exceeds MAX_LIFE, throws an integer exception. If life > 15 
//prints “Boxer Wins”. If life is less than or equal to 5, the function  
//announces the boxer is knocked out, and sets life to 0. If none of the 
//previous conditions are met, the function adds 5 to the boxer's life and 
//announces it. 
void boxer(int &life)
{
    if (life > MAX_LIFE)
        throw MAX_LIFE;
    else if (life > 15)
        throw UPPERCUT;
    else if (life > 10)
        throw HOOK;
    else if (life > 5)
        throw JAB;
    else
    {
        cout << "Boxer knocked out" << endl;
        life = 0;
    }
    catch (Punch p)     
    {
        if (p == UPPERCUT)
            cout << "Boxer Wins" << endl;
        else 
        {
            life + 5;
            cout << "Boxer's life count is now " << life << endl;
        }
    }
}

// EFFECTS: throws a Ball exception if catcher_sign and
// pitcher_choice are the same, throws a bool exception
// otherwise.
void pitcher(Ball catcher_sign, Ball pitcher_choice)
{
    if (pitcher_choice = catcher_sign) throw pitcher_choice;
        throw false;
}

// EFFECTS: Prints “Strike” if sign matches pitcher_wants, prints “Something 
//got caught” if sign does not match pitcher_wants, or if boxer_life exceeds 
//MAX_LIFE.
void throw_things(Ball sign, Ball pitcher_wants, int boxer_life)
{
    pitcher(sign, pitcher_wants);
    try 
    {
        boxer(boxer_life);
    }
    catch (...) 
    {
        cout << "Something got caught" << endl;
    }
    catch (Ball b) 
    {
        cout << "Strike" << endl;
    }
}


------------
I came across this code while looking online for practice for my upcoming exam. The question is, Identify any sources of error in the above functions and explain how they would be corrected.

So... Here is what I have.

Line 34) Change life + 5 to life+=5.
Line 45) Change the = to ==.
Line 59/63) Put the default handler below the catch Ball b, so it is last.

Now the two questions I have...
Line 54) Should the call to pitcher go inside the try block? If I had to say yes or no, I'd say yes. All the stuff I've found online has functions that have the possibility to throw, inside a try block.

Line 28) Boxer doesn't have a try. But since the call is in a try block in lines 55 to 58, it doesn't need one? If I had to say yes or no, I'd say no. Because I feel like although there isn't a try in boxer itself, the throws will go out of boxer and down to the catch blocks in throw_things. I feel like its nested, if you know what I mean.

(Any other errors I am missing?!)

Thanks!


Last edited on
Should the call to pitcher go inside the try block?
Yes.

Line 28) Boxer doesn't have a try. But since the call is in a try block in lines 55 to 58, it doesn't need one?
It needs one, because catch() without try isn't allowed. That there's a try within throw_things() doesn't matter for boxer(). They might be logically nested but not structurally.

Any other errors I am missing?!
line 52: boxer_life is not a reference -> the modification in boxer() gets lost
Last edited on
Topic archived. No new replies allowed.