1

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?

6
  • 2
    OT: Why on earth you have a method named read that actually does the opposite: writes?? Commented Aug 1, 2017 at 14:30
  • "Redirect to the constructor" ? What do you mean? The constructor always executes first, and your read method that writes gets executed later. How do you imagine that you could "redirect" something that happens later to something that happened earlier? Time travel? Commented Aug 1, 2017 at 14:32
  • Please see the updated post. The read() reads an input file and then I write a message in a verbose file. Commented Aug 1, 2017 at 14:32
  • @ErwinBolwidt: OK. So what is the good way to handle that? As I said, in multiple methods, I want to use verb.write(). Commented Aug 1, 2017 at 14:34
  • @mahmood I don't see much sens in the question. You want to redirect exception and handle it later, but don't what to handle it in your main method. What I've understood incorrectly? Commented Aug 1, 2017 at 14:36

1 Answer 1

1

One approach is to make your own "safe" wrapper around BufferedWriter (for that matter, any kind of Writer) and handle I/O errors there:

class SafeWriter {
    private final Writer writer;
    public SafeWriter(Writer writer) {
        this.writer = writer;
    }
    public void write(int n) {
        try {
            writer.write(n);
        } catch (IOException e) {
            handleException(e);
        }
    }
    public void write(String s) {
        try {
            writer.write(s);
        } catch (IOException e) {
            handleException(e);
        }
    }
    ... // Provide wrappers for other methods here
    private void handleException(IOException e) {
        ...
    }
}

Now you can use write methods on your new class to handle exceptions in a uniform way inside the class:

private SafeWriter verb;
...
verb = new SafeWriter(new BufferedWriter(new FileWriter(vFile)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.