Why am I not able to use throws IOException in my method which uses toRealPath(), instead I have to use try catch?

40 views Asked by At
import java.io.IOException;
import java.nio.file.*;

public class Rose {

    public void tendGarden(Path p) throws IOException {
        Files.walk(p,1)
                .map(q -> q.toRealPath())
                .forEach(System.out::println);
    }

    public static void main(String[] thorns) throws IOException {
        new Rose().tendGarden(Paths.get(thorns[0]));
    }
}

The above code doesn't compile because of q.toRealPath(), instead throws java: unreported exception java.io.IOException; must be caught or declared to be thrown. while I am already throwing it to main method which is throwing it to java environment. Somewhere I read that because of using interface as a method signature I am not able to use throws. Can someone please explain why?

0

There are 0 answers