Add try-finally block to ensure removal of FCP listener.
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / FcpClient.java
index bcf529b..21dc647 100644 (file)
@@ -142,18 +142,21 @@ public class FcpClient {
                        }
                };
                fcpConnection.addFcpListener(fcpListener);
-               fcpConnection.connect();
-               ClientHello clientHello = new ClientHello(name);
-               fcpConnection.sendMessage(clientHello);
-               while (true) {
-                       try {
-                               fcpListener.complete();
-                               break;
-                       } catch (InterruptedException e) {
-                               /* ignore, we’ll loop. */
+               try {
+                       fcpConnection.connect();
+                       ClientHello clientHello = new ClientHello(name);
+                       fcpConnection.sendMessage(clientHello);
+                       while (true) {
+                               try {
+                                       fcpListener.complete();
+                                       break;
+                               } catch (InterruptedException e) {
+                                       /* ignore, we’ll loop. */
+                               }
                        }
+               } finally {
+                       fcpConnection.removeFcpListener(fcpListener);
                }
-               fcpConnection.removeFcpListener(fcpListener);
                if (fcpListener.getFcpException() != null) {
                        throw fcpListener.getFcpException();
                }