gpt4 book ai didi

java - 我们如何处理 ConnectException?

转载 作者:行者123 更新时间:2023-12-03 19:04:45 29 4
gpt4 key购买 nike

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

将抛出此异常的 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;
}
}
}

我使用 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);

}}

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

堆栈跟踪:

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.access$400(MainFrame.java:34)
at ClientGUI.MainFrame$5.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)

最佳答案

您在上一个问题中发布了代码:Sign in button has no visible effect in a Java application

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

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

编辑:我会稍微扩展一下。 ConnectException延伸RemoteException它又扩展了 IOException,您正在捕获它并将其记录到您的 runAClient 方法中。你可以做两件事:

  • 完全删除 IOException,并在上层捕获它(在我看来这是一个非常丑陋的解决方案)

  • IOException block 拆分为两个 catch block ,一个用于 ConnectException,您刚刚在其中抛出,另一个用于其余的 IOException,您可以记录(或以不同方式处理)- 只需确保 ConnectException block 在 IOException block
  • 之前写入

关于java - 我们如何处理 ConnectException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2042247/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com