如何捕获异常并继续Java中的处理 [英] how to catch exceptions and continue the processing in Java

查看:254
本文介绍了如何捕获异常并继续Java中的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我在循环中处理5000个文件到6000个文件。



在try和catch块中我正在读取excel文件并处理每个文件单个单元格。



当然所有文件都采用相同的格式,但在某些文件中,单元格中的数据可能会有所不同,但可能包含数据



当处理第100个文件时出现异常,
整个处理停止并抛出异常,



<但是我不想要这种情况,相反,如果在第100个文件中存在异常,则迭代应继续使用第101个文件。
最后我应该知道哪个文件是成功处理的,哪个文件是失败的。



我得到的例外是
NumberFormatException NullPointerExceptions



如何处理这种情况?

解决方案

基本思路是将try-catch块放在循环中。

  for(文件文件:文件){
try {
parseExcelFile(file); //对文件执行任何操作
}
catch(异常e){
logger.warn(解析文件时出错:+ file,e);
}
}


I have an applicaton where I am processing 5000 files to 6000 files during a loop.

In a try and catch block I am reading the excel file and processing each individual cell.

Of course all the Files are in the same format, but In some files the data in the cell in may vary it may contain data or not

when ever there is an exception while processing 100th file, the whole processing is stopped and exception is thrown,

But I dont want that scenario, instead if there is an exception at 100th file, the iteration should continue with 101th file. And in the end I should know which file is processed succesfully and which one is failed.

Exception which I am gettng are NumberFormatException and NullPointerExceptions

How to hand that scenario?

解决方案

The basic idea is to put the try-catch block inside the loops.

for (File file : files) {
    try {
        parseExcelFile(file); // Do whatever you want to do with the file
    }
    catch (Exception e) {
        logger.warn("Error occurs while parsing file : " + file, e);
    }
}

这篇关于如何捕获异常并继续Java中的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆