Java 为什么我的编译器总是说我有一个catch块而没有以前的try块

Java 为什么我的编译器总是说我有一个catch块而没有以前的try块,java,exception,methods,try-catch,Java,Exception,Methods,Try Catch,因此,我的代码如下所示: try { t.delete("word"); result = t.getRootItem().getWord().equals("humpty"); } catch (Exception e) { result = false; } 问题是,我的编译器一直说我有一个捕获,但我没有以前的尝试,但我有以前的尝试,那么有什么错?这是我的整个主要方法(如果您愿意,我也可以发布整个类): public static void main(String

因此,我的代码如下所示:

try {
    t.delete("word");
    result = t.getRootItem().getWord().equals("humpty");
} catch (Exception e) {
    result = false;
}
问题是,我的编译器一直说我有一个捕获,但我没有以前的尝试,但我有以前的尝试,那么有什么错?这是我的整个主要方法(如果您愿意,我也可以发布整个类):

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"));
}

这一行似乎不正确:

t = new BSTRefBased
构造函数调用没有
()
,也没有分号。它紧跟在
try
之前,这些错误一定会扰乱解析器,使其无法再识别
try
。try

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

您的代码看起来很好-问题可能在其他地方(可能是这样的字段声明后缺少了一个
)。