[Coverity] Fix Coverity issue at KNN Application
authorDonghak PARK <donghak.park@samsung.com>
Thu, 6 Feb 2025 01:19:14 +0000 (10:19 +0900)
committerjijoong.moon <jijoong.moon@samsung.com>
Tue, 11 Feb 2025 05:45:07 +0000 (14:45 +0900)
Uncaught exception (UNCAUGHT_EXCEPT)
- root_function: In function main(int, char **) an exception of type std::bad_cast is thrown and never caught
- to fix issue, add try catch statement at writefile

**Self evaluation:**
1. Build test:  [X]Passed [ ]Failed [ ]Skipped
2. Run test:  [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Donghak PARK <donghak.park@samsung.com>
Applications/KNN/jni/main.cpp

index 8f74c10a98ba98609d914c5d66f6576a60066268..e42dd24f2b834d08e41e643b93aae2b8fa4c9176 100644 (file)
@@ -207,11 +207,18 @@ int main(int argc, char *argv[]) {
       std::string out_file = "/sdcard/Transfer-Learning/";
       out_file += total_label[i] + std::to_string(j + 1) + ".txt";
       printf("%s\n", out_file.c_str());
-      std::ofstream writeFile(out_file.data());
-      if (writeFile.is_open()) {
-        for (int k = 0; k < 128; k++)
-          writeFile << out[i][j][k] << std::endl;
-        writeFile.close();
+      try {
+        std::ofstream writeFile(out_file.data());
+        if (writeFile.is_open()) {
+          for (int k = 0; k < 128; k++)
+            writeFile << out[i][j][k] << std::endl;
+          writeFile.close();
+        } else {
+          throw std::runtime_error("Failed to open file : " + out_file);
+        }
+      } catch (const std::exception &e) {
+        std::cerr << "Exception caught while writing to file : " << e.what()
+                  << std::endl;
       }
     }
   }