當我嘗試從方法聲明中拋出一個異常時,我得到錯誤「ClassNotFoundException的Unreachable catch塊,這個異常永遠不會從try語句體中拋出。我可以從方法中捕獲拋出異常嗎?
的代碼是這樣的:
public class MenuSQL {
private static String sentence = "";
private static int option;
Statement sentenceSQL = ConnectSQL.getConexion().createStatement();
public MenuSQL(int option) throws ClassNotFoundException, SQLException {
super();
this.option = option;
try {
System.out.print("Introduce the sentence: ");
System.out.print(sentence);
sentence += new Scanner(System.in).nextLine();
System.out.println(MenuSentence.rightNow("LOG") + "Sentence: " + sentence);
if (opcion == 4) {
MenuSentence.list(sentence);
} else {
sentenceSQL.executeQuery(sentence);
}
} catch (SQLException e) {
System.out.println(MenuSentence.rightNow("SQL") + "Sentence: " + sentence);
} catch (ClassNotFoundException e) {
System.out.println(MenuSentence.rightNow("ERROR") + "Sentence: " + sentence);
}
}
}
我如何能趕上ClassNotFoundException
?提前致謝。
爲什麼你想要捕捉一個永遠不會拋出你的'try'block的異常?爲什麼你的方法拋出異常,但它捕捉到它們? –
該方法內部沒有任何可以引發ClassNotFoundException的點。 –
這是因爲編譯器迫使我在方法 –