为什么getOrElse会抛出NoSuchElementException(仅在Intellij调试器中)?

时间:2016-09-23 17:44:06

标签: scala intellij-idea

"点"调用getOrElse是为了允许一个元素不在Map中 - 并在这种情况下提供一个默认值:

  /**  Returns the value associated with a key, or a default value if the key is not contained in the map.
   *   @param   key      the key.
   *   @param   default  a computation that yields a default value in case no binding for `key` is
   *                     found in the map.
   *   @tparam  B1       the result type of the default computation.
   *   @return  the value associated with `key` if it exists,
   *            otherwise the result of the `default` computation.
   *
   *   @usecase def getOrElse(key: A, default: => B): B
   *     @inheritdoc
   */
  def getOrElse[B1 >: B](key: A, default: => B1): B1 = get(key) match {

那么为什么NoSuchElementException会抛出以下代码:

  // nid = "LPKey-FToS"
  val nstateOld = state.activeNodes.getOrElse(nid, emptyNodeState)

以下是例外:

java.util.NoSuchElementException: key not found: LPKey-FToS
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:58)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:58)
    at myClass$1.apply(MyClass.scala:162)

更新显然只有在单步执行断点后才能在intellij中调试时才会出现此。我正在为此问题添加Intellij-IDEA标记。

0 个答案:

没有答案