Java捕获错误并继续运行到下一个

Java捕获错误并继续运行到下一个,java,Java,我找不到一种方法来保持我的程序在捕获并出错后运行。 例如,我有: String[] num={"1","2","3","NotNumber","4","5"}; 我想把所有的都转换成整数,所以num[3]是无效的,但我想在捕捉到错误后继续运行到num[4]和num[5]。 我该怎么做呢?如果您已经展示了到目前为止所做的尝试,这会有所帮助,但最简单的解决方案是将您的int.parse()包装在try/catch块中并吞下异常 for(int i = 0; i < items.

我找不到一种方法来保持我的程序在捕获并出错后运行。 例如,我有:

String[] num={"1","2","3","NotNumber","4","5"};       
我想把所有的都转换成整数,所以
num[3]
是无效的,但我想在捕捉到错误后继续运行到
num[4]
num[5]

我该怎么做呢?

如果您已经展示了到目前为止所做的尝试,这会有所帮助,但最简单的解决方案是将您的
int.parse()
包装在
try/catch
块中并吞下异常

for(int i = 0; i < items.length; i++) {
    try {
        newItems[i] = Itemger.parseInt(items[i]);
    catch(Exception ex) {
        // do nothing
    }
}
for(int i=0;i
try catch
块放入迭代中

JAVA 7

List<Integer> intList = new ArrayList<>();
for(String s : num) {
  try {
    Integer n = Integer.valueOf(s);
    intList.add(n);
  } catch (Exception ex) { continue; }
}
List intList=new ArrayList();
for(字符串s:num){
试一试{
整数n=整数。值为(s);
intList.add(n);
}catch(异常ex){continue;}
}
Java8流

List<Integer> intList = Arrays.asList(num)
  .stream()
  .map(s -> {
    try {
      return Integer.valueOf(s);
    } catch(Exception ex) { return null;}
  })
  .filter(i -> i != null)
  .collect(Collectors.toList());
List intList=Arrays.asList(num)
.stream()
.map(s->{
试一试{
返回整数。值为(s);
}catch(异常ex){returnnull;}
})
.filter(i->i!=null)
.collect(Collectors.toList());
试试这段代码,我相信它会管用的。