How come the following code prints Success(4) if i'm throwing an exception inside?

前端 未结 1 1184
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 08:15
object MyRealMainObj extends App {

  println(
    Try(1)
      .map(doOne)
      .map(doTwo)

  )

  def doOne(i: Int): Int = i + 1; throw new RuntimeException(\"failed         


        
相关标签:
1条回答
  • 2021-01-27 08:46

    wrap the code inside curly braces like,

    def doOne(i: Int): Int = {i + 1; throw new RuntimeException("failed in one")}
    

    The throw new RuntimeException("failed in one") is not within the function it is a separate line inside main flow.

    0 讨论(0)
提交回复
热议问题