Problema com IOException?

Estava vendo aulas de Java e quando fui fazer um exemplo deu um erro, e eu tô realmente tentando entender o motivo mas não consigo.

O código:

import java.nio.file.*;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;


public class AulaXTI63{
   public static void main (String[] args){
   
      Path p = Paths.get("C:/JavaEX/texto.txt");
      BufferedWriter w = null;
      Charset utf8 = StandardCharsets.UTF_8;
   
   try{
      
      w = Files.newBufferedWriter(p, utf8);
      w.write("Hello, it's me!\n");
      w.write("Hello baby");
      w.flush();
   
   
   }catch(IOException e){
      
      e.printStackTrace();
      
   }finally{
      
      if(w != null) {
         w.close();
      }
    }
  }
}

O erro:

AulaXTI63.java:29: error: unreported exception IOException; must be caught or declared to be thrown
w.close();
^
1 error

Tenta isso:

public static void main (String[] args) throws IOException{
      Path p = Paths.get("C:/JavaEX/texto.txt");
      BufferedWriter w = null;
      Charset utf8 = StandardCharsets.UTF_8;
   try{
      w = Files.newBufferedWriter(p, utf8);
      w.write("Hello, it's me!\n");
      w.write("Hello baby");
      w.flush();
   }catch(IOException e){
      e.printStackTrace();
   }finally{
      if(w != null) {
         w.close();
      }
    }
  }
2 curtidas

Deu certo, obrigado. Mas pensei que por estar usando a estrutura try-catch não precisaria colocar o throws no método.

Vlw o/