-1

How can one stop the IOException question being raised on something as such:

public class ExampleClass {

   String path = "path to directory";
   Path filePath = Paths.get(path);
   BasicFileAttributes Attr = Files.readAttributes(filePath, BasicFileAttributes.class);

   ...

}

I receive a warning from IntelliJ stating Unhandled exception: java.io.IOException on the last line BasicFileAttributes inAttr = Files.readAttributes(filePathIn, BasicFileAttributes.class);

If I put these declarations within a method that throws an IOException, the warning does go away... But I am wanting this declared as a global variable... Am I missing something?

physicsboy
  • 5,656
  • 17
  • 70
  • 119

1 Answers1

0

You can circumvent that by initializing Attr inside the constructor (wrapped in a try-catch block).

public ExampleClass(){
    try {
            Attr = Files.readAttributes(Paths.get(""), BasicFileAttributes.class);
    } catch (IOException e) {
            e.printStackTrace();
    }
}
lher
  • 126
  • 7
  • I understand doing something like that, or by placing in a method... Was just curious if there was any way around it. – physicsboy Nov 16 '17 at 14:56
  • Finding a way around it is pointless, since it would make the concept of checked and unchecked exceptions obsolete. Look at the link of Mickaël B. – lher Nov 16 '17 at 15:12