Exceptions and referential transparency

Reading "Functional Programming in Scala" and I'm a bit confused by the section on exceptions that are not transparent.

Given example

def failingFn(i: Int): Int = {
  val y: Int = throw new Exception("fail!")
  try {
    val x = 42 + 5
    x + y
  }
  catch { case e: Exception => 43 }
}

So, the argument given in the book is that yit is not referentially transparent, because if we replace it with the body in the block try, we will get a different result than just starting the function directly. It does not make any sense to me, because the whole function does not end, to start with, what is the point of expression of values ​​inside the body of the function, is not transparently transparent? A naive substitute in my mind would be this:

def failingFn(i: Int): Int = {
  val y: Int = throw new Exception("fail!")
  try {
    val x = 42 + 5
    x + ((throw new Exception("fail!")): Int)
  }
  catch { case e: Exception => 43 }
}

and still not executed with the same exception.

, y ( ), ? , - , , ?

+4
1

, , , , y try/catch, .

, , .

, , , - , . y lazy, .

+4

All Articles