0

If I have two functions A() and B() where A calls B. Now if an exception is thrown inside B why can't I easily catch it inside A using a try catch block in A.

func B () -> Void {
   // library code that triggers an unhandled fatal exception
}

func A () -> Void {
  do {
    try B()
  } catch {
    print(error)
  }
}

This is something very easily done in other languages such as Java/Scala/Javascript etc.

tusharmath
  • 10,622
  • 12
  • 56
  • 83
  • That has nothing to do with error propagation. "Index out of bounds" is a *runtime exception* and not a Swift error, it cannot be catched. – Martin R Mar 10 '18 at 08:22
  • My question is not about index out of bounds specifically but its more on the lines of how to gracefully handle any exception in swift code. – tusharmath Mar 10 '18 at 08:39
  • But the answer still applies, you cannot catch "any exception" in Swift, only *errors* which are (explicitly) *thrown.* See also https://stackoverflow.com/questions/38737880/uncaught-error-exception-handling-in-swift. – Martin R Mar 10 '18 at 09:01

0 Answers0