Loại exception nào có thể xảy ra khi đóng con trỏ tới file?

Cụ thể trong method dưới đây, tui không rõ dòng lệnh writer.close(); có thể xảy ra lỗi gì để bắt nên chỉ biết để chung chung exception. Thậm chí tui còn không biết cách nào để thử cho nó lỗi nữa. Mong mọi người chỉ bảo.
Xin cảm ơn.

public void writeToFile(String filename){
    PrintWriter writer = null;
    try{
        writer = new PrintWriter(filename);
        
    } catch ( FileNotFoundException e){
        System.out.println("Find not found.");
    } finally{
        try{
            if(writer != null){
                writer.close();
            }
        }catch( Exception e){
            System.out.println("Something went wrong.");
        }
    }
}

Coi document của nó có throw Exception không? Nếu không, thì khỏi catch. Còn nếu vẫn nghi ngờ, đọc source code (nếu có).
https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#close()
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/io/PrintWriter.java#l334

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError() .

8 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?