How can we handle the ConnectException?
Solution 1
You posted the code in your previous question: Sign in button has no visible effect in a Java application
Judging by the runAClient
method, you are logging the exception and eating it up (not throwing it further).
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.
Edit: I'll extend this a bit. ConnectException
extends RemoteException
which in turn extends IOException
, which you are catching and logging in your runAClient
method. You can do two things:
- remove the
IOException
altogether, and catching it at an upper level (a solution which is very ugly in my opinion)
or
- split the
IOException
block into two catch blocks, one for theConnectException
, in which you just throw, and one for the rest of theIOException
s, which you can log (or handle differently) - just make sure theConnectException
block is written before theIOException
block
Solution 2
Do you have the code for
Main.runAClient();
perhaps somebody is catching that exception and logging it, and then rethrowing?
Johanna
Updated on June 15, 2022Comments
-
Johanna about 2 years
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.
submit() method which will throw this exception:
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:
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:
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.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)
-
laura over 14 yearsIndeed it is being logged, see here: stackoverflow.com/questions/2041817/gui-problemnetwork-problem
-
Bozho over 14 years(+1), just be explicit about ConnectionException extending IOException, hence being caught.
-
Adamski over 14 years@Johanna: What exactly are you asking? Laura has shown how to restructure your code to not swallow the exception. Are you asking why the exception is actually happening?