Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未报告的IOException,即使在try-catch块中并引发异常_Java_Ioexception - Fatal编程技术网

Java 未报告的IOException,即使在try-catch块中并引发异常

Java 未报告的IOException,即使在try-catch块中并引发异常,java,ioexception,Java,Ioexception,我的代码有未报告的IOException,即使我在try-catch块中有它,并且在方法名处抛出IOException public static void addStudent() throws Exception { try { final JFrame frame = new JFrame("Add Details"); frame.setSize(350, 200); frame.setDefaultCloseOperati

我的代码有未报告的IOException,即使我在try-catch块中有它,并且在方法名处抛出IOException

public static void addStudent() throws Exception
{
    try
    {
        final JFrame frame = new JFrame("Add Details");
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(
          JFrame.EXIT_ON_CLOSE);

        final JTextField tf1 = new JTextField("Input Student's name here", 25);
        final JTextField tf2 = new JTextField("Input Student's ID number here", 25);
        final JTextField tf3 = new JTextField("Input Student's Email Address here", 25);
        final JTextField tf4 = new JTextField("Input Student's address here", 25);
        JButton submit = new JButton("Submit");
        JButton back = new JButton("Back");
        JPanel panel = new JPanel();
        panel.add(tf1);
        panel.add(tf2);
        panel.add(tf3);
        panel.add(tf4);
        panel.add(submit);
        panel.add(back);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
        submit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) 
                    {
                        String name = tf1.getText();
                        String ID = tf2.getText();
                        String email = tf3.getText();
                        String address = tf4.getText();
                        String fileName = ID + "Details.csv";
                        File file = new File(fileName);
                        FileWriter fw = new FileWriter(fileName); 
                        name = name.concat(",");
                        ID = ID.concat(",");
                        email = email.concat(",");
                        address = address.concat(",");
                        fw.write(name);
                        fw.write(ID);
                        fw.write(email);
                        fw.write(address);
                        fw.close();
                    }
                });


    back.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    frame.dispose();
                    GUI.AdminGUI();
                }
            });
        }
        catch(Exception e)
            {
                System.exit(0);
            }
}

有人知道为什么会这样吗?我不明白为什么每当您在编写Java代码时遇到错误时就会抛出异常,无论它是在您尝试编译或运行代码时发生的,都要特别注意错误发生的位置(例如,行号,在哪个方法中发生)。并与你寻求帮助的人分享


我最好的猜测是java在抱怨,因为您的
addStudent
方法抛出
Exception
以及代码中其他地方使用
addStudent
的部分(可能是您的
main
方法)无法处理引发的此可能的
异常。

请尝试将
Try/catch
块放入
actioPerformed()
中。我认为您的捕获可能不在
actionPerformed()


如果您这样做,您不需要使用
addStudent()
来抛出任何异常,也不需要它的
try/catch
块。

我对编程非常陌生,请您告诉我它看起来有多奇怪,我将不胜感激。谢谢你的建议。我试试看你的代码没有处理IOException。不要只是猜测编译器说了什么。准确地引用它。准确地阅读它。它告诉你问题所在。我照你说的做了,我得到了这个错误:错误:找不到符号fw.close();只需将它放回
的底部,然后尝试将它放回原来的位置,最后去掉
块。如果您最后输入
,它将不在范围内。很抱歉我修好了。
submit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) 
    {
        try {
             String name = tf1.getText();
             String ID = tf2.getText();
             String email = tf3.getText();
             String address = tf4.getText();
             String fileName = ID + "Details.csv";
             File file = new File(fileName);
             FileWriter fw = new FileWriter(fileName); 
             name = name.concat(",");
             ID = ID.concat(",");
             email = email.concat(",");
             address = address.concat(",");
             fw.write(name);
             fw.write(ID);
             fw.write(email);
             fw.write(address);
             fw.close();
        }
        catch (IOException ex){
             ex .printStackTrace();
             System.exit(0);
        }          
    }
});