簡體   English   中英

Java未報告的異常使我感到困惑

[英]Java unreported exception confusing me

大家好,我一直在為GUI設計一些按鈕,我決定實施一些以前的代碼。

但是,嘗試編譯時出現錯誤。 在我的代碼的第141行(特別是最后一個按鈕)中,我被告知我有一個未報告的IOException ,必須將其捕獲或聲明為拋出。

我的代碼如下:

public void actionPerformed(ActionEvent ae) {
    if ((ae.getSource() == button5) && (!connected)) {
        try {
            s = new Socket("127.0.0.1", 2020);
            pw = new PrintWriter(s.getOutputStream(), true);
        } catch (UnknownHostException uhe) {
            System.out.println(uhe.getMessage());
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
        connected = true;
        t = new Thread(this);
        //b.setEnabled(false);
        button5.setLabel("Disconnect");
        t.start();
    } else if ((ae.getSource() == button5) && (connected)) {
        connected = false;
        try {
            s.close(); //no buffering so, ok
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
        //System.exit(0);
        button5.setLabel("Connect");
    } else {
        temp = tf.getText();
        pw.println(temp);
        tf.setText("");
    }
    if (ae.getActionCommand().equals("Save it")) {
        Scanner scan = new Scanner(System.in);
        try {
            PrintWriter pw = new PrintWriter(new FileWriter(new File("test.txt")));
            for (;;) {
                String temp = scan.nextLine();
                if (temp.equals("")) {
                    break;
                }
                pw.println(temp);
            }
            pw.close();
        } catch (IOException ioe) {
            System.out.println("IO Exception! " + ioe.getMessage());
        }
    } else if (ae.getActionCommand().equals("Load it")) {
        Scanner scan = new Scanner(System.in);
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File("test.txt")));
            String temp = "";
            while ((temp = br.readLine()) != null) {
                System.out.println(temp);
            }
            br.close();
        } catch (FileNotFoundException fnfe) {
            System.out.println("Input file not found.");
        } catch (IOException ioe) {
            System.out.println("IO Exception! " + ioe.getMessage());
        }
    } else if (ae.getActionCommand().equals("Clear it")) {
        ta.setText("");
    } else {
        PrintWriter pw = new PrintWriter(new FileWriter(new File("test.txt")));

    }
}

只需在以下代碼中添加try / catch塊(發布內容的結尾):

else{
PrintWriter pw = new PrintWriter (
            new FileWriter(
            new File("test.txt")));

}}

像這樣:

else{
        try{
            PrintWriter pw = new PrintWriter (new FileWriter(new File("test.txt")));
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
}}

通常,任何IO操作都可能導致異常。 根據您的需要,最簡單的解決方案是throws IOException到您看到問題的方法的頂部,但這不是很好的做法,在這種情況下不起作用。 將try / catch塊放在問題線上,並包含有意義的錯誤消息,可能是最好的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM