我可以从方法中捕获抛出异常吗?
Can I catch a throw exception from method?
当我尝试从方法声明中抛出异常时,出现错误 "Unreachable catch block for ClassNotFoundException. This exception is never thrown from the try statement body"。
代码是这样的:
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{...} catch(){...}
语句的catch块只能捕获try{...}
块抛出的异常。 (或该异常的超类)
try {
Integer.parseInt("1");
//Integer.parseInt throws NumberFormatException
} catch (NumberFormatException e) {
//Handle this error
}
但是,您要做的基本上是这样的:
try {
Integer.parseInt("1");
//Integer.parseInt throws NumberFormatException
} catch (OtherException e) {
//Handle this error
}
因为 none 你的 try{...}
块中的语句抛出 OtherException
,编译器会给你一个错误,因为它知道 nothing 在你的 try{...}
块中将 ever 抛出那个异常,所以你不应该尝试 catch
永远不会 thrown
.[=22 的东西=]
在你的例子中,你的 try{...}
块中没有任何东西会抛出 ClassNotFoundException
,所以你不需要捕捉它。您可以从代码中删除 catch (ClassNotFoundException e) {...}
以修复错误。
当我尝试从方法声明中抛出异常时,出现错误 "Unreachable catch block for ClassNotFoundException. This exception is never thrown from the try statement body"。
代码是这样的:
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{...} catch(){...}
语句的catch块只能捕获try{...}
块抛出的异常。 (或该异常的超类)
try {
Integer.parseInt("1");
//Integer.parseInt throws NumberFormatException
} catch (NumberFormatException e) {
//Handle this error
}
但是,您要做的基本上是这样的:
try {
Integer.parseInt("1");
//Integer.parseInt throws NumberFormatException
} catch (OtherException e) {
//Handle this error
}
因为 none 你的 try{...}
块中的语句抛出 OtherException
,编译器会给你一个错误,因为它知道 nothing 在你的 try{...}
块中将 ever 抛出那个异常,所以你不应该尝试 catch
永远不会 thrown
.[=22 的东西=]
在你的例子中,你的 try{...}
块中没有任何东西会抛出 ClassNotFoundException
,所以你不需要捕捉它。您可以从代码中删除 catch (ClassNotFoundException e) {...}
以修复错误。