为什么我的编译器一直说我有一个没有之前 try 块的 catch 块
Why does my compiler keep saying I have a catch block without a previous try block
所以我的代码是这样的:
try {
t.delete("word");
result = t.getRootItem().getWord().equals("humpty");
} catch (Exception e) {
result = false;
}
问题是我的编译器一直说我没有事先尝试就成功了,但我确实有过尝试,这是怎么回事?这是我的整个主要方法(如果需要,我也可以 post 整个 class:
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
。尝试
t = new BSTRefBased(); // or a similar constructor call
所以我的代码是这样的:
try {
t.delete("word");
result = t.getRootItem().getWord().equals("humpty");
} catch (Exception e) {
result = false;
}
问题是我的编译器一直说我没有事先尝试就成功了,但我确实有过尝试,这是怎么回事?这是我的整个主要方法(如果需要,我也可以 post 整个 class:
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
。尝试
t = new BSTRefBased(); // or a similar constructor call