Consider the following code
public class foo {
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.read();
}
}
and
public class MyClass {
private BufferedWriter verb;
private String vFile;
MyClass()
{
try {
verb = new BufferedWriter(new FileWriter(vFile));
} catch(IOException e) {
System.out.println("Internal error1");
System.out.println(e.getMessage());
}
}
public void read()
{
// read a file and create an array
verb.write("Array created"); // ERROR
}
}
As you can see the write is not placed in a try..catch block. I can write a catch for that, but MyClass has many methods and verb.write is used heavily. I also can write public void read() throws IOException to throw the exception to the the caller, main(). Still I have to put mc.read() in a try..catch block. Since MyClass has numerous methods, then I have to put all of them in a catch block in the main().
So, is there a better way to handle that? Is it possible to redirect all exceptions related to verb to the constructor, MyClass() where a try..catch is defined?
readmethod that writes gets executed later. How do you imagine that you could "redirect" something that happens later to something that happened earlier? Time travel?read()reads an input file and then I write a message in a verbose file.verb.write().