transporthandler: Fix out_of_range exception caused by using at

When using the .at method to get the value in std::map, if the key
does not exist, an out_of_range exception will be thrown.

This commit moves this logic to the try method and catches the
exception.

Signed-off-by: George Liu <liuxiwei@ieisystem.com>
Change-Id: Ia9479bd3ce53d32515bef13db1bba7b3fe6e0429
diff --git a/transporthandler.hpp b/transporthandler.hpp
index a2137cd..3285017 100644
--- a/transporthandler.hpp
+++ b/transporthandler.hpp
@@ -257,38 +257,39 @@
 {
     for (const auto& [path, properties] : ips)
     {
-        std::optional<typename AddrFamily<family>::addr> addr;
         try
         {
-            addr.emplace(stdplus::fromStr<typename AddrFamily<family>::addr>(
-                std::get<std::string>(properties.at("Address"))));
+            typename AddrFamily<family>::addr addr;
+            addr = stdplus::fromStr<typename AddrFamily<family>::addr>(
+                std::get<std::string>(properties.at("Address")));
+
+            sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin
+                origin = sdbusplus::server::xyz::openbmc_project::network::IP::
+                    convertAddressOriginFromString(
+                        std::get<std::string>(properties.at("Origin")));
+            if (origins.find(origin) == origins.end())
+            {
+                continue;
+            }
+
+            if (idx > 0)
+            {
+                idx--;
+                continue;
+            }
+
+            IfAddr<family> ifaddr;
+            ifaddr.path = path;
+            ifaddr.address = addr;
+            ifaddr.prefix = std::get<uint8_t>(properties.at("PrefixLength"));
+            ifaddr.origin = origin;
+
+            return ifaddr;
         }
         catch (...)
         {
             continue;
         }
-
-        sdbusplus::server::xyz::openbmc_project::network::IP::AddressOrigin
-            origin = sdbusplus::server::xyz::openbmc_project::network::IP::
-                convertAddressOriginFromString(
-                    std::get<std::string>(properties.at("Origin")));
-        if (origins.find(origin) == origins.end())
-        {
-            continue;
-        }
-
-        if (idx > 0)
-        {
-            idx--;
-            continue;
-        }
-
-        IfAddr<family> ifaddr;
-        ifaddr.path = path;
-        ifaddr.address = *addr;
-        ifaddr.prefix = std::get<uint8_t>(properties.at("PrefixLength"));
-        ifaddr.origin = origin;
-        return ifaddr;
     }
 
     return std::nullopt;
@@ -358,31 +359,33 @@
             Neighbor::State::Permanent);
     for (const auto& [path, neighbor] : neighbors)
     {
-        std::optional<typename AddrFamily<family>::addr> neighIP;
         try
         {
-            neighIP.emplace(stdplus::fromStr<typename AddrFamily<family>::addr>(
-                std::get<std::string>(neighbor.at("IPAddress"))));
+            typename AddrFamily<family>::addr neighIP;
+            neighIP = stdplus::fromStr<typename AddrFamily<family>::addr>(
+                std::get<std::string>(neighbor.at("IPAddress")));
+
+            if (neighIP != ip)
+            {
+                continue;
+            }
+            if (state != std::get<std::string>(neighbor.at("State")))
+            {
+                continue;
+            }
+
+            IfNeigh<family> ret;
+            ret.path = path;
+            ret.ip = ip;
+            ret.mac = stdplus::fromStr<stdplus::EtherAddr>(
+                std::get<std::string>(neighbor.at("MACAddress")));
+
+            return ret;
         }
         catch (...)
         {
             continue;
         }
-        if (*neighIP != ip)
-        {
-            continue;
-        }
-        if (state != std::get<std::string>(neighbor.at("State")))
-        {
-            continue;
-        }
-
-        IfNeigh<family> ret;
-        ret.path = path;
-        ret.ip = ip;
-        ret.mac = stdplus::fromStr<stdplus::EtherAddr>(
-            std::get<std::string>(neighbor.at("MACAddress")));
-        return ret;
     }
 
     return std::nullopt;