2010-09-02 114 views
4

即使处理一组文件中的某些文件时发生异常,我如何使用异常和异常处理来使我的程序继续运行?Java异常处理

我希望我的程序能正常工作,而对于那些在程序中导致异常的文件,它应该忽略。

问候,

magggi

+0

非常感谢您的回答。 – Magggi 2010-09-02 17:51:40

回答

14
for(File f : files){ 
    try { 
     process(f); // may throw various exceptions 
    } catch (Exception e) { 
     logger.error(e.getMessage(), e); 
    } 
} 
+1

对于有意义的日志消息(包括文件名)+1。我看到的大多数代码只有'logger.info(e.getMessage())'。 – Thilo 2010-09-02 09:34:04

+1

不捕获一般Exception类型,而是捕获特定异常类型(IOException,...)可能会更好。 – boutta 2010-09-02 09:34:13

+2

尽管在实践中,如果打开文件连接,您将需要添加一个finally块,以便根据Colins的回答关闭连接。 – Joel 2010-09-02 09:35:51

11

你不得不使用的try/catch /终于集团。

try{ 
    //Sensitive code 
} catch(ExceptionType e){ 
    //Handle exceptions of type ExceptionType or its subclasses 
} finally { 
    //Code ALWAYS executed 
} 
  • try将让你执行敏感代码可能抛出异常。
  • catch将处理特定异常(或此异常的任何子类型)。
  • finally将帮助执行语句,即使抛出异常并且未捕捉到异常。

在你的情况

for(File f : getFiles()){ 
    //You might want to open a file and read it 
    InputStream fis; 
    //You might want to write into a file 
    OutputStream fos; 
    try{ 
     handleFile(f); 
     fis = new FileInputStream(f); 
     fos = new FileOutputStream(f); 
    } catch(IOException e){ 
     //Handle exceptions due to bad IO 
    } finally { 
     //In fact you should do a try/catch for each close operation. 
     //It was just too verbose to be put here. 
     try{ 
      //If you handle streams, don't forget to close them. 
      fis.close(); 
      fos.close(); 
     }catch(IOException e){ 
      //Handle the fact that close didn't work well. 
     } 
    } 
} 

资源:

+0

我知道这一点。 问题:1.我的应用程序处理多个文件。 (例外) 3.但现在我的应用程序退出任何1个文件失败。 4.我想我的应用程序继续执行其他文件。那么我应该如何构建我的异常处理? – Magggi 2010-09-02 09:22:46

+2

你应该把你所有的异常处理放在循环中。 – 2010-09-02 09:30:22

0

只是抓住了它可能抛出的异想天开而无所作为;像人们说的那样吃它:) 但是至少要登录它!

非常简洁例如:

try { 
    your code... 
} catch (Exception e) { 
    log here 
} 
2

我猜你的新的编程为execeptions是一个相当fundermental概念,问题可能发生在你的控制,你需要处理它。

基本前提是try catch块。

try 
{ 
    //Your code here that causes problems 
} 
catch(exception ex) 
{ 
    //Your code to handle the exception 
} 

您'尝试'您的代码,并且如果引发异常,您'捕捉'它。并做你需要的。 此外还有一个catch块,您可以在其下面添加{}。基本上,即使没有引发异常,最终的代码仍然运行。您可能想知道这一点,但它经常用于流/文件处理等来关闭流。

了解更多关于Java异常在这里由Sun(现在是Oracle)编写的教程 - http://download.oracle.com/javase/tutorial/essential/exceptions/

try 
{ 
    //Your code here that causes problems 
} 
catch(exception ex) 
{ 
    //Your code to handle the exception 
} 
finally 
{ 
    //Always do this, i.e. try to read a file, catch any errors, always close the file 
} 

你可能会问是怎么做的,你捕获不同的例外,即它是一个空引用,是它的问题除以零,找不到文件或文件不可写入等。为此,您可以在try下编写几个不同的catch块,基本上每种类型的异常都有一个catch,使用“exception”基本上就是catch all语句,就像在if语句堆栈中一样,如果“exception”是第一个catch阻止它会捕获所有内容,所以如果你有几个catch块,确保异常是最后一个。

同样,这是一个有用但很大的话题,所以你需要阅读它。

由于你正在做多个文件,你需要基本上做一个循环,并在循环内包含try/catch块。

所以即使一个文件失败了,你也可以抓住它,但是继续运行,代码将会不受阻碍地循环到下一个文件。

0

通常,我会这样做。

ArrayList<Entry> allEntries = getAllEntries(); 

for(Entry eachEntry:allEntries){ 
    try{ 
    //do all your processing for eachEntry 
    } catch(Exception e{ 
    ignoredEntries.add(eachEntry); 
    //if concerned, you can store even the specific problem. 
    } finally{ 
    //In case of resource release 
    } 
} 

if(ignoredEntries.size() > 0){ 
    //Handle this scenario, may be display the error to the user 
} 
0

FileSystemException可能是您正在寻找的特定异常。

虽然,对初学者更好的主意是捕捉异常,并使用

System.out.println(e);

其中e是捕捉到的异常打印。