Exception handling question
Hi,
I have written a tool in java. To run the tool I do:
MyTool tool = new MyTool();
tool.process();
In my code there is risk that code will throw IOException since it
will not be able to read a file. Or it can be a
TransformerConfigurationException in my xsl transformer. If a step
fails I would like stop continuing the processing since all other
further steps will be failing.
I am thinking of doing something like:
try {
MyTool tool = new MyTool();
tool.process();
} catch (MyToolException mte){
mte.printStackTrace();
System.exit(0);
}
Example of code in tool that gives a IOException:
private static void copy(String from, String to) {
try {
File f1 = new File(from);
File f2 = new File(to);
InputStream in = new FileInputStream(f1);
// For Overwrite the file.
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied from: " + f1 + " to: " +
f2);
} catch (FileNotFoundException ex) {
throw new MyToolException("File Could not be found");
} catch (IOException e) {
throw new MyToolException("File Could not be read");
}
}
Or is there an other way to do this? I am using jdk1.4.2 ( and I am
stuck with it :-( ).
br,
//mike