Java: error handling with try-catch, empty-try-catch, dummy-return
- by HH
A searh uses recursively defined function that easily throws exceptions. I have tried 3 ways to handle exeptions:
to ignore with an empty-try-catch()
add-dummy-return stop err-propagation due to exeption
throw a specific except. (this part I don't really understand. If I throw except, can I force it to continue elsewhere, not continuing the old except-thrown-path?)
Some exceptions I do not realy care like during execution removed files -exception (NullPointer) but some I really do like unknown things.
Possible exceptions:
// 1. if a temp-file or some other file removed during execution -> except.
// 2. if no permiss. -> except.
// 3. ? --> except.
The code is Very import for the whole program. I earlier added clittered-checks, try-catches, avoided-empty-try-catches but it really blurred the logic. Some stoned result here would make the code later much easier to maintain. It was annoying to track random exeptions due to some random temp-file removal! How would you handle exceptions for the critical part?
Code
public class Find
{
private Stack<File> fs=new Stack<File>();
private Stack<File> ds=new Stack<File>();
public Stack<File> getD(){ return ds;}
public Stack<File> getF(){ return fs;}
public Find(String path)
{
// setting this type of special checks due to errs
// propagation makes the code clittered
if(path==null)
{
System.out.println("NULL in Find(path)");
System.exit(9);
}
this.walk(path);
}
private void walk( String path )
{
File root = new File( path );
File[] list = root.listFiles();
//TODO: dangerous with empty try-catch?!
try{
for ( File f : list ) {
if ( f.isDirectory() ) {
walk( f.getAbsolutePath() );
ds.push(f);
}
else {
fs.push(f);
}
}
}catch(Exception e){e.printStackTrace();}
}
}
Code refactored from here.