Java 在try内返回,返回空列表时会通过catch,但不会';如果列表不为空,则无法编译
我有以下代码: 导入java.util.*Java 在try内返回,返回空列表时会通过catch,但不会';如果列表不为空,则无法编译,java,Java,我有以下代码: 导入java.util.* public class InterviewPerp { public static void main(String[] args) { something(); } public static List<String> something() { String digits = "23"; List<String> combinations = ne
public class InterviewPerp {
public static void main(String[] args) {
something();
}
public static List<String> something() {
String digits = "23";
List<String> combinations = new ArrayList<>();
try {
if(Integer.parseInt(digits) <= 1)
//combinations.add("*");
return combinations;
} catch(NumberFormatException nfe) {
return combinations;
}
return combinations;
}
}
公共课堂访谈者{
公共静态void main(字符串[]args){
某物();
}
公共静态列表(){
字符串数字=“23”;
列表组合=新的ArrayList();
试一试{
if(Integer.parseInt(digits)或者将代码放在if like下面的括号中
if(Integer.parseInt(digits) <= 1)
{
combinations.add("*");
return combinations;
}
if(Integer.parseInt(digits)参考代码和注释
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
something();
}
public static List<String> something() {
String digits = "23";
List<String> combinations = new ArrayList<>();
try {
if (Integer.parseInt(digits) <= 1)
combinations.add("*");
return combinations; // this code will be executed always if there is no error thus the code after the catch statement will never be reached, generating an error
} catch (NumberFormatException nfe) {
return combinations;
}
// The code below here will not be executed
// Code to calculate combinations
return combinations;
}
}
但一般来说,为了可读性,只需将catch块后的所有代码传输到try块内。因为在catch块后添加代码可能意味着或其他人可能会读取这些代码,因为这些代码将在catch块执行后执行。发布MCVE。这不是一个。我无法重现您的错误。不过,您可以/应该这样做将return
语句移到其他地方。发布其余代码。此处缺少大括号。if(Integer…return组合;按如下方式更正它。.if(Integer.parseInt(digits)是的,我认为可能是{}
在您从问题中忽略的if
之后。在堆栈溢出时,如果您发布导致问题的代码,而不是完全不同的代码,通常会得到更好的答案。
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
something();
}
public static List<String> something() {
String digits = "23";
List<String> combinations = new ArrayList<>();
try {
if (Integer.parseInt(digits) <= 1)
combinations.add("*");
return combinations; // this code will be executed always if there is no error thus the code after the catch statement will never be reached, generating an error
} catch (NumberFormatException nfe) {
return combinations;
}
// The code below here will not be executed
// Code to calculate combinations
return combinations;
}
}
if (Integer.parseInt(digits) <= 1){
combinations.add("*");
return combinations;
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
something();
}
public static List<String> something() {
String digits = "23";
List<String> combinations = new ArrayList<>();
try {
if (Integer.parseInt(digits) <= 1)
combinations.add("*");
// Code to calculate combinations is transferred here
return combinations;
} catch (NumberFormatException nfe) {
return combinations;
}
// The code below are transferred inside the try block
}
}