unreported IOException even though in try-catch block and throws Exception

StackOverflow https://stackoverflow.com/questions/20185009

  •  04-08-2022
  •  | 
  •  

Вопрос

My code has unreported IOExceptions even though I have it in a try-catch block and at the method name I have throws 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);
            }
}

Can anyone see why this is happening? I don't understand why the exception is being thrown

Это было полезно?

Решение

Try putting the try/catch block inside the actioPerformed(). I think you're catch may not be within the scope of the actionPerformed()

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);
        }          
    }
});

If you do this, you don't need to the addStudent() to throw any exception and you don't need its try/catch block.

Другие советы

Whenever you get an error in writing Java code, whether it happens when you try to compile or run the code, always take note of specifically where the error happens (e.g. the line number, which method(s) this happens in). And share it with the people you ask for help.

My best guess is that java is complaining because your addStudent method throws Exception and the part elsewhere in your code that uses addStudent (likely your main method) doesn't handle this possible Exception that is thrown.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top