java 我们如何处理 ConnectException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2042247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:06:19  来源:igfitidea点击:

How can we handle the ConnectException?

javanetworking

提问by Johanna

I have searched a lot ,but i couldn't find the fine answer for it.I use try_catch block for this exception(if this exception is thrown one frame will be shown to the user that I will tell him/her a message) but it still show the exception in the console.please help me.Thanks.

我已经搜索了很多,但我找不到合适的答案。我使用 try_catch 块来处理这个异常(如果抛出这个异常,将向用户显示一帧,我会告诉他/她一条消息)但是它仍然在控制台中显示异常。请帮助我。谢谢。

submit() method which will throw this exception:

submit() 方法将抛出此异常:

  private void submit() throws ConnectException {

    String id = idField.getText();
    char[] pass1 = passField.getPassword();
    String pass = new String(pass1);
    if (id.equals("") || pass.equals("")) {
        JOptionPane.showMessageDialog(this, "You should enter an ID and password", "Sign_In Problem", JOptionPane.OK_OPTION);
        return;
    } else {
        boolean b = Manager.Test(id, pass);
        if (b == true) {
            this.setVisible(false);

            Main.runAClient();

            ListFrame frame = new ListFrame(client);
            frame.setVisible(true);





        } else {

            JOptionPane.showMessageDialog(this, "You have entered wrong datas,try it again", "Sign_In Problem", JOptionPane.OK_OPTION);
            return;
        }
    }
}

I work with netbeans,this is an action for sign in button:

我使用 netbeans,这是登录按钮的操作:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        submit();
    } catch (ConnectException ex) {
        JOptionPane.showMessageDialog(this, "You coudn't connect to the server successfully,try it again", "Sign_In Problem", JOptionPane.OK_OPTION);

    }}

my runAClient method:

我的 runAClient 方法:

 public static void runAClient()throws ConnectException{
    try {
        c = new Socket("localhost", 5000);
    } catch (ConnectException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

stacktrace:

堆栈跟踪:

init:
deps-jar:
compile-single:
run-single:
Jan 11, 2010 5:20:35 PM ClientNetWork.Main runAClient
SEVERE: null
java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:518)
        at java.net.Socket.connect(Socket.java:468)
        at java.net.Socket.<init>(Socket.java:365)
        at java.net.Socket.<init>(Socket.java:179)
        at ClientNetWork.Main.runAClient(Main.java:29)
        at ClientGUI.MainFrame.submit(MainFrame.java:335)
        at ClientGUI.MainFrame.jButton1ActionPerformed(MainFrame.java:233)
        at ClientGUI.MainFrame.access0(MainFrame.java:34)
        at ClientGUI.MainFrame.actionPerformed(MainFrame.java:122)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
        at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(BasicRootPaneUI.java:191)
        at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
        at javax.swing.JComponent.processKeyBinding(JComponent.java:2849)
        at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:267)
        at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:216)
        at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2926)
        at javax.swing.JComponent.processKeyBindings(JComponent.java:2918)
        at javax.swing.JComponent.processKeyEvent(JComponent.java:2812)
        at java.awt.Component.processEvent(Component.java:5815)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4410)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
        at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:697)
        at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:962)
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:834)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:661)
        at java.awt.Component.dispatchEventImpl(Component.java:4282)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Window.dispatchEventImpl(Window.java:2429)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

回答by laura

You posted the code in your previous question: Sign in button has no visible effect in a Java application

您在上一个问题中发布了代码:登录按钮在 Java 应用程序中没有可见效果

Judging by the runAClientmethod, you are logging the exception and eating it up (not throwing it further).

从该runAClient方法来看,您正在记录异常并吃掉它(而不是进一步抛出它)。

public static void runAClient() {
    try {
        c = new Socket("localhost", 5000);
    } catch (UnknownHostException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The second catch (IOException) should not log and should throw the exception. Actually, you probably want to split that catch into a ConnectException catch which you would throw and a IOException catch which you can handle separately.

第二个 catch ( IOException) 不应该记录并且应该抛出异常。实际上,您可能希望将该捕获拆分为可以抛出的 ConnectException 捕获和可以单独处理的 IOException 捕获。

Edit: I'll extend this a bit. ConnectExceptionextends RemoteExceptionwhich in turn extends IOException, which you are catching and logging in your runAClientmethod. You can do two things:

编辑:我会稍微扩展一下。ConnectExceptionextendsRemoteException依次扩展IOException,您正在捕获并记录您的runAClient方法。你可以做两件事:

  • remove the IOExceptionaltogether, and catching it at an upper level (a solution which is very ugly in my opinion)
  • IOException完全删除,并在上层捕获它(在我看来这是非常丑陋的解决方案)

or

或者

  • split the IOExceptionblock into two catch blocks, one for the ConnectException, in which you just throw, and one for the rest of the IOExceptions, which you can log (or handle differently) - just make sure the ConnectExceptionblock is written before the IOExceptionblock
  • IOException块分成两个 catch 块,一个用于ConnectException,您只是在其中抛​​出,另一个用于IOExceptions的其余部分,您可以记录(或以不同方式处理) - 只需确保ConnectException块写在IOException块之前

回答by djna

Do you have the code for

你有代码吗

  Main.runAClient();

perhaps somebody is catching that exception and logging it, and then rethrowing?

也许有人正在捕获该异常并记录它,然后重新抛出?