Throw and catch exceptions for less bug prone lambda termination

Previously, the initial comparison for mApfFilter == null called
completeExceptionally() without actually breaking out of the lambda.

In order to prevent this bug in the future, it is better to actually
throw and catch the exception.

This also slightly rewords an exception message to make it fit on one
line.

Test: adb shell cmd network_stack apf wlan0 status
Change-Id: Ia7502cc74b3a0498fc4a14d9560acb60c6286cf9
diff --git a/src/android/net/ip/IpClient.java b/src/android/net/ip/IpClient.java
index 017fdd8..ed72289 100644
--- a/src/android/net/ip/IpClient.java
+++ b/src/android/net/ip/IpClient.java
@@ -1417,45 +1417,45 @@
         final CompletableFuture<String> result = new CompletableFuture<>();
 
         getHandler().post(() -> {
-            if (mApfFilter == null) {
-                // IpClient has either stopped or the interface does not support APF.
-                result.completeExceptionally(
-                        new IllegalStateException("No active APF filter."));
-            }
-            switch (cmd) {
-                case "status":
-                    result.complete(mApfFilter.isRunning() ? "running" : "paused");
-                    break;
-                case "pause":
-                    mApfFilter.pause();
-                    result.complete("success");
-                    break;
-                case "resume":
-                    mApfFilter.resume();
-                    result.complete("success");
-                    break;
-                case "install":
-                    if (optarg == null) {
-                        result.completeExceptionally(
-                                new IllegalArgumentException("No program provided"));
-                    } else if (mApfFilter.isRunning()) {
-                        result.completeExceptionally(
-                                new IllegalStateException("APF filter must be paused for install"));
-                    } else {
-                        mCallback.installPacketFilter(HexDump.hexStringToByteArray(optarg));
+            try {
+                if (mApfFilter == null) {
+                    // IpClient has either stopped or the interface does not support APF.
+                    throw new IllegalStateException("No active APF filter.");
+                }
+                switch (cmd) {
+                    case "status":
+                        result.complete(mApfFilter.isRunning() ? "running" : "paused");
+                        break;
+                    case "pause":
+                        mApfFilter.pause();
                         result.complete("success");
-                    }
-                    break;
-                case "capabilities":
-                    final StringJoiner joiner = new StringJoiner(",");
-                    joiner.add(Integer.toString(mCurrentApfCapabilities.apfVersionSupported));
-                    joiner.add(Integer.toString(mCurrentApfCapabilities.maximumApfProgramSize));
-                    joiner.add(Integer.toString(mCurrentApfCapabilities.apfPacketFormat));
-                    result.complete(joiner.toString());
-                    break;
-                default:
-                    result.completeExceptionally(
-                            new IllegalArgumentException("Invalid apf read command: " + cmd));
+                        break;
+                    case "resume":
+                        mApfFilter.resume();
+                        result.complete("success");
+                        break;
+                    case "install":
+                        if (optarg == null) {
+                            throw new IllegalArgumentException("No program provided");
+                        } else if (mApfFilter.isRunning()) {
+                            throw new IllegalStateException("APF filter must first be paused");
+                        } else {
+                            mCallback.installPacketFilter(HexDump.hexStringToByteArray(optarg));
+                            result.complete("success");
+                        }
+                        break;
+                    case "capabilities":
+                        final StringJoiner joiner = new StringJoiner(",");
+                        joiner.add(Integer.toString(mCurrentApfCapabilities.apfVersionSupported));
+                        joiner.add(Integer.toString(mCurrentApfCapabilities.maximumApfProgramSize));
+                        joiner.add(Integer.toString(mCurrentApfCapabilities.apfPacketFormat));
+                        result.complete(joiner.toString());
+                        break;
+                    default:
+                        throw new IllegalArgumentException("Invalid apf read command: " + cmd);
+                }
+            } catch (Exception e) {
+                result.completeExceptionally(e);
             }
         });