GregH
GregH

Reputation: 12858

Why does instantiating a new FileHandler not work in my java program?

I'm trying to create my own logging class by using java.util.logging. Part of this class allows the caller to specify a log file using the FileHandler class. However, I can't seem to get one of my methods to create a new FileHandler object. Here is basically what I have:

import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.FileHandler;

public class myLogger {
  private FileHandler logFileHdl = null; 

  ...
  public void setLogFilename(String filename) {
     this.logFileHdl = new FileHandler(filename)
     ...
  }

When I run this I get: "java: unreported exception java.io.IOException".

I also get this same error if I get rid of the setLogFilename method and just try to create new FileHandler object when I declare the logFileHdl attribute like:

import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.FileHandler;

public class myLogger {
  private FileHandler logFileHdl = new FileHandler(); 

I'm not sure why.

Upvotes: 0

Views: 447

Answers (1)

GregH
GregH

Reputation: 12858

Ok, so question was because I am a Java newbie. Apparently because the FileHandler constructor can throw an IOException, I am forced to handle the exception (or declare that I my class can throw that exception.) I didn't realized that was the case. So if I add a "try/catch" for IOException then the error goes away.

Upvotes: 1

Related Questions