本文介绍了如何将其他信息添加到可以稍后检索的异常对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我需要获取确切的原因,无论是FileNotFound还是普通的IOException.我的意思是如果它是Filenotfound异常,请不要松开FilenotFound原因.如果是其他IOException,请显示IOException.没有覆盖原因.我需要准确地描述问题,巩固例外情况
重要:我需要以这种必需的方式编写代码块样式.
原始代码块必须以这种方式遵循,除此之外,没有要求

In Java, I need to get the exact causes whether is FileNotFound or normal IOException. Which I mean if it is Filenotfound exception, do not loose the FilenotFound cause. If it is other IOException, show IOException. No overwritten the causes.
I need to accurately describe the problem,consolidate the exceptions
Important: I need the code block style in this required way.
The original code block must follow in this way, beyond this, no requirement

main(){
try {
readfile();
}catch {
}
}
readfile(){
try{...
}
finally{
if (br1 != null)
br1.close();
}
}


上面的模板会导致问题,最终br1.close可能会通过生成IOException覆盖FileNotException.
这是我完整的代码:


Above template cause a problem, in finally br1.close could possibly overwrite the FileNotException by generating a IOException.
Here is my complete code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

class Hidden {
  public static void main(String args[]){
    Hidden h = new Hidden();

    try {
    	System.out.println("Main():: Try Block Starts");
    	h.readFile();
    	System.out.println("Main():: Try Block Ends");
    } catch (FileNotFoundException fne) {
    	System.out.println("Caught FileNotFoundException!");
    	System.out.println(fne.getMessage());
    	  //fne.printStackTrace();
    } catch (IOException ioe) {
    	System.out.println("Caught IOException!");
  	  	System.out.println(ioe.getCause());
  	  //ioe.printStackTrace();
    }
  }

  public void readFile() throws FileNotFoundException,IOException {
	  BufferedReader br1 = null;
      FileReader fr = null;

      try {
    	  System.out.println("readFile():: Try Block Starts.");
    	  fr = new FileReader("data1.fil");                       //1
    	  br1 = new BufferedReader(fr);
    	 // int i = br1.read();                                     //2
    	  //Other code...
    	  System.out.println("readFile():: Try Block Ends.");                                   //4
     } catch (IOException ioe){
    	 System.out.println("readFile():: Catch Block Starts.");
    	 if(ioe.getCause() instanceof FileNotFoundException);
    	  throw new FileNotFoundException("data1.fil is not exisited");
    }
     finally {
    	System.out.println("readFile():: Finally Block Starts");
    	if (br1 != null) {
    		br1.close();
    	}else {
    		//throw new IOException();
        }
    	System.out.println("readFile():: Finally Block Ends");
    }
  }
}

推荐答案

FileNotFoundException saveException = null;
try {
  readfile();    // might throw FileNotFoundException
} catch (FileNotFoundException fnfEx) {
  saveException = new FileNotFoundException(fnfEx);
} finally {
  try {
    br1.close();   // might throw IOException, but we don't care about it
  } finally {
    if (saveException != null)
      throw saveException;
  {
}



这样做是为了在处理任何IOException时有效隐藏FileNotFoundException,然后在发生异常时再次将其抛出.
有点混乱,但是可以用.

干杯,
彼得
如果这回答了您的问题,请接受.仍要投票.



What this does is to effectively hide the FileNotFoundException while you deal with any IOException then throws it again if it occurred.
It''s a bit messy, but it works.

Cheers,
Peter
If this answers your question, accept it. Vote anyway.


这篇关于如何将其他信息添加到可以稍后检索的异常对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:28