Java异常处理
回答
for(File f : files){
try {
process(f); // may throw various exceptions
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
你不得不使用的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.
}
}
}
资源:
我知道这一点。 问题:1.我的应用程序处理多个文件。 (例外) 3.但现在我的应用程序退出任何1个文件失败。 4.我想我的应用程序继续执行其他文件。那么我应该如何构建我的异常处理? – Magggi 2010-09-02 09:22:46
你应该把你所有的异常处理放在循环中。 – 2010-09-02 09:30:22
只是抓住了它可能抛出的异想天开而无所作为;像人们说的那样吃它:) 但是至少要登录它!
非常简洁例如:
try {
your code...
} catch (Exception e) {
log here
}
我猜你的新的编程为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块。
所以即使一个文件失败了,你也可以抓住它,但是继续运行,代码将会不受阻碍地循环到下一个文件。
通常,我会这样做。
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
}
FileSystemException
可能是您正在寻找的特定异常。
虽然,对初学者更好的主意是捕捉异常,并使用
System.out.println(e);
其中e
是捕捉到的异常打印。
- 1. Java异常处理
- 2. Java异常处理
- 3. 处理java异常
- 4. java异常处理
- 5. Java异常处理
- 6. Java异常处理方法
- 7. Java EE/Struts异常处理
- 8. Infinity的Java异常处理
- 9. Java SpringBoot异常处理
- 10. Java异常处理策略
- 11. 核心-java异常处理
- 12. Java中的异常处理
- 13. Java异常处理查询
- 14. Java中的异常处理
- 15. Java异常处理概念
- 16. Java异常处理:/ zeo
- 17. Java异常处理机制
- 18. WP7 - 处理基于异步JAVA的SOAP服务异常处理
- 19. 异常处理
- 20. 处理异常
- 21. 异常处理
- 22. 异常处理
- 23. 异常处理
- 24. 异常处理
- 25. 处理异常
- 26. 异常处理
- 27. 异常处理
- 28. 异常处理
- 29. 异常处理
- 30. 异常处理?
非常感谢您的回答。 – Magggi 2010-09-02 17:51:40