Skip to content

Exceptions II - Challenge 2 #235

@BryanF1nes

Description

@BryanF1nes

In Exceptions II - Challenge 2 should doesSomethingDangerous already have a throws Exception attached to it? Or is this up to the individual to recognize?

Current written structure:

/*The following program will not compile. Make it compile by 
propagating the checked exception thrown 
by the doesSomethingDangerous method.
*/

int doesSomethingDangerous(int value) {
    if (value == 1) {
        throw new Exception("1 does not work");
    }
    return 3 * value + 2;
}

int compute(int start) {
    return square(doesSomethingDangerous(start));
}

int square(int x) {
    return x * x;
}

void main() {
    int x = Integer.parseInt(IO.readln("Give a starting number: "));
    IO.println(compute(x));
}

Potential line fix.

int doesSomethingDangerous(int value) throws Exception {
    if (value == 1) {
        throw new Exception("1 does not work");
    }
    return 3 * value + 2;
}

int compute(int start) {
    return square(doesSomethingDangerous(start));
}

int square(int x) {
    return x * x;
}

void main() {
    int x = Integer.parseInt(IO.readln("Give a starting number: "));
    IO.println(compute(x));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions