John Kyle
John Kyle

Reputation: 117

Why does my compiler keep saying I have a catch block without a previous try block

So my code looks like this:

try {
    t.delete("word");
    result = t.getRootItem().getWord().equals("humpty");
} catch (Exception e) {
    result = false;
}

The problem is my compiler keeps saying I have a catch without a previous try but I do have a previous try so what's wrong? Here's my entire main method (I can also post the entire class if you want:

public static void main(String args[]) {
    BSTRefBased t;
    AbstractBinaryTree tt;
    int i;
    boolean result;
    String message;

    message = "Test 1: inserting 'word0' -- ";
    t = new BSTRefBased();
    try {
        t.insert("word0");
        result = t.getRootItem().getWord().equals("word0");
    } catch (Exception e) {
        result = false;
    }
    System.out.println(message + (result ? "passed" : "FAILED"));

    message = "Test 2: inserting 'word1', 'word2', 'word3' -- ";
    t = new BSTRefBased();
    try {
        t.insert("word1");
        t.insert("word2");
        t.insert("word3");
        result = t.getRootItem().getWord().equals("word1");
        tt = t.detachLeftSubtree();
        result &= tt.getRootItem().getWord().equals("word2");
        tt = t.detachRightSubtree();
        result &= tt.getRootItem().getWord().equals("word3");
    } catch (Exception e) {
        result = false;
    }
    System.out.println(message + (result ? "passed" : "FAILED"));

    message = "Test 3: deleting 'word3'";
    t = new BSTRefBased
    try {
        t.delete("word3");
        result = t.getRootItem().getWord().equals("word3");
    } catch (Exception e) {
        result = false;
    }
    System.out.println(message + (result ? "passed" : "FAILED"));
}

Upvotes: 1

Views: 40

Answers (1)

rgettman
rgettman

Reputation: 178253

This line appears to be incorrect:

t = new BSTRefBased

There is no () for a constructor call, and there is no semicolon. It's immediately before the try, and those errors must be messing up the parser, such that it no longer recognizes the try. Try

t = new BSTRefBased();  // or a similar constructor call

Upvotes: 1

Related Questions