Separates UnsupportedSdkException from BenchmarkException.
am: 9aaf234c1b

Change-Id: I2075b75511c9ae044a677981e92c44659a7e9d74
diff --git a/src/com/android/nn/benchmark/app/BenchmarkTestBase.java b/src/com/android/nn/benchmark/app/BenchmarkTestBase.java
index a079ad8..b09016a 100644
--- a/src/com/android/nn/benchmark/app/BenchmarkTestBase.java
+++ b/src/com/android/nn/benchmark/app/BenchmarkTestBase.java
@@ -25,6 +25,7 @@
 
 import androidx.test.InstrumentationRegistry;
 
+import com.android.nn.benchmark.core.BenchmarkException;
 import com.android.nn.benchmark.core.BenchmarkResult;
 import com.android.nn.benchmark.core.TestModels;
 import com.android.nn.benchmark.core.TestModels.TestModelEntry;
@@ -120,7 +121,7 @@
             try {
                 mResult = mActivity.mProcessor.getInstrumentationResult(
                     mTestModel, mWarmupTimeSeconds, mRunTimeSeconds);
-            } catch (IOException e) {
+            } catch (BenchmarkException | IOException e) {
                 mException = e;
                 e.printStackTrace();
             }
diff --git a/src/com/android/nn/benchmark/app/NNBenchmark.java b/src/com/android/nn/benchmark/app/NNBenchmark.java
index b9c43c5..100ac57 100644
--- a/src/com/android/nn/benchmark/app/NNBenchmark.java
+++ b/src/com/android/nn/benchmark/app/NNBenchmark.java
@@ -31,6 +31,7 @@
 import com.android.nn.benchmark.core.InferenceResult;
 import com.android.nn.benchmark.core.NNTestBase;
 import com.android.nn.benchmark.core.TestModels;
+import com.android.nn.benchmark.core.UnsupportedSdkException;
 
 import java.util.List;
 import java.io.IOException;
@@ -84,7 +85,7 @@
         // Method to retrieve benchmark results for instrumentation tests.
         BenchmarkResult getInstrumentationResult(
                 TestModels.TestModelEntry t, float warmupTimeSeconds, float runTimeSeconds)
-                throws IOException {
+                throws BenchmarkException, IOException {
             mTest = changeTest(t);
             return getBenchmark(warmupTimeSeconds, runTimeSeconds);
         }
@@ -114,43 +115,43 @@
 
         // Get a benchmark result for a specific test
         private BenchmarkResult getBenchmark(float warmupTimeSeconds, float runTimeSeconds)
-                throws IOException {
+            throws BenchmarkException, IOException {
             try {
                 mTest.checkSdkVersion();
-
-                mDoingBenchmark = true;
-
-                long result = 0;
-
-                // We run a short bit of work before starting the actual test
-                // this is to let any power management do its job and respond.
-                // For NNAPI systrace usage documentation, see
-                // frameworks/ml/nn/common/include/Tracing.h.
-                try {
-                    final String traceName = "[NN_LA_PWU]runBenchmarkLoop";
-                    Trace.beginSection(traceName);
-                    runBenchmarkLoop(warmupTimeSeconds, false);
-                } finally {
-                    Trace.endSection();
-                }
-
-                // Run the actual benchmark
-                BenchmarkResult r;
-                try {
-                    final String traceName = "[NN_LA_PBM]runBenchmarkLoop";
-                    Trace.beginSection(traceName);
-                    r = runBenchmarkLoop(runTimeSeconds, mCompleteInputSet);
-                } finally {
-                    Trace.endSection();
-                }
-
-                Log.v(TAG, "Test: " + r.toString());
-
-                mDoingBenchmark = false;
-                return r;
-            } catch (BenchmarkException e) {
+            } catch (UnsupportedSdkException e) {
                 return new BenchmarkResult(e.getMessage());
             }
+
+            mDoingBenchmark = true;
+
+            long result = 0;
+
+            // We run a short bit of work before starting the actual test
+            // this is to let any power management do its job and respond.
+            // For NNAPI systrace usage documentation, see
+            // frameworks/ml/nn/common/include/Tracing.h.
+            try {
+                final String traceName = "[NN_LA_PWU]runBenchmarkLoop";
+                Trace.beginSection(traceName);
+                runBenchmarkLoop(warmupTimeSeconds, false);
+            } finally {
+                Trace.endSection();
+            }
+
+            // Run the actual benchmark
+            BenchmarkResult r;
+            try {
+                final String traceName = "[NN_LA_PBM]runBenchmarkLoop";
+                Trace.beginSection(traceName);
+                r = runBenchmarkLoop(runTimeSeconds, mCompleteInputSet);
+            } finally {
+                Trace.endSection();
+            }
+
+            Log.v(TAG, "Test: " + r.toString());
+
+            mDoingBenchmark = false;
+            return r;
         }
 
         @Override
@@ -201,7 +202,12 @@
                             warmupTime = 2.f;
                             runTime = 10.f;
                         }
-                        mTestResults[ct] = getBenchmark(warmupTime, runTime);
+                        try {
+                            mTestResults[ct] = getBenchmark(warmupTime, runTime);
+                        } catch (BenchmarkException e) {
+                            // Displays the error to the user.
+                            mTestResults[ct] = new BenchmarkResult(e.getMessage());
+                        }
                     }
                     onBenchmarkFinish(mRun);
                 } catch (IOException e) {
diff --git a/src/com/android/nn/benchmark/core/NNTestBase.java b/src/com/android/nn/benchmark/core/NNTestBase.java
index 2e202ac..d6dfc95 100644
--- a/src/com/android/nn/benchmark/core/NNTestBase.java
+++ b/src/com/android/nn/benchmark/core/NNTestBase.java
@@ -136,9 +136,9 @@
         return mEvaluator;
     }
 
-    public void checkSdkVersion() throws BenchmarkException {
+    public void checkSdkVersion() throws UnsupportedSdkException {
         if (mMinSdkVersion > 0 && Build.VERSION.SDK_INT < mMinSdkVersion) {
-            throw new BenchmarkException("SDK version not supported. Mininum required: " +
+            throw new UnsupportedSdkException("SDK version not supported. Mininum required: " +
                     mMinSdkVersion + ", current version: " + Build.VERSION.SDK_INT);
         }
     }
diff --git a/src/com/android/nn/benchmark/core/UnsupportedSdkException.java b/src/com/android/nn/benchmark/core/UnsupportedSdkException.java
new file mode 100644
index 0000000..f232ee2
--- /dev/null
+++ b/src/com/android/nn/benchmark/core/UnsupportedSdkException.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.nn.benchmark.core;
+
+public class UnsupportedSdkException extends Exception {
+  public UnsupportedSdkException(String message) {
+    super(message);
+  }
+};