Click here to Skip to main content
15,889,649 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
main(){
try {
readfile();
}catch {
}
}
readfile(){
try{...
}
finally{
if (br1 != null) 
br1.close();
}
}

Above template cause a problem, in finally br1.close could possibly overwrite the FileNotException by generating a IOException.
Here is my complete code:
Java
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");
    }
  }
}
Posted
Updated 19-May-11 18:43pm
v4

1 solution

Since Exception has what is effectively a copy constructor, you can do this:

Java
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;
  {
}


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.
 
Share this answer
 
Comments
TorstenH. 20-May-11 3:21am    
looks fine to me: +5!
In addition to that: a proper Exception Handling is always needed. You can also extend the exceptions to your needs - for example I have seen Exceptions with an ExceptionID to make it easier to see where the Exception has been thrown.
Peter_in_2780 20-May-11 3:28am    
Yeah. I miss the C(++) preprocessor where I could use a macro to autonumber exceptions or even do things like throw new MyException(__LINE__, __FILE__) :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900