4

I am using a for-loop to iterate an array. When the index is out of bounds, the exception breakpoint is not stopping at the point of error.

My code

class ViewController: UIViewController
{
    let integers = [1, 2, 3]

    override func viewDidLoad()
    {
        super.viewDidLoad()
        iterate(givenArray: integers)
    }

    func iterate(givenArray: [Int])
    {
        for index in 0...givenArray.count
        {
            print(givenArray[index])
        }
    }
}

I am getting the following error at the console.

1 2 3 Fatal error: Index out of range 2020-01-06 14:29:09.726533+0530 Playground[93060:2221772] Fatal error: Index out of range

I understand the error. My question is that though I have added the Exception breakpoint, why is it not working?

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
  • That's a little confusing, because when you see that error message and your program execution stops, it has... well... effectively done the same thing a ***breakpoint*** would do. What purpose would it serve to have that caught, essentially, by *another* breakpoint? – DonMag Jan 28 '20 at 13:58
  • @DonMag When the program execution stops due to a crash, you will only get an error message. A breakpoint, on the other hand, will take you exactly to the code line. – Nilanshu Jaiswal Jan 29 '20 at 04:03

2 Answers2

0

It is not breaking with an Exception breakpoint because a call to fatalError() is not the same as throwing an exception - it is more severe.

You can consider it an indication that your app is in a state it should not try to recover from, and will always result in your app exiting.

There are creative ways[1] of breaking on a call to fatalError, but I think you are better off considering this a code error that should be fixed, and guarding yourself against the error that way, not trying to handle the error in runtime.

[1] - Break on any occurrence of "fatal error: unexpectedly found nil while unwrapping an Optional value"

Terje
  • 980
  • 9
  • 15
  • The exception breakpoint was working for the same code in the previous version of the Xcode. So, can we achieve the same in this version of Xcode through some other means? Is the exception breakpoint working for any other code error in this updated version of Xcode? Or is this an Xcode bug? – Nilanshu Jaiswal Jan 29 '20 at 04:06
0

Remember that errors that occur in Swift are not exceptions, but simply errors (NSError, regardless of which is based on ErrorType, ...).

Do not mix a symbolic breakpoint with an exception breakpoint. Different animals. Keep this in mind. When you try to set an exception checkpoint in Xcode, there is no Swift. Probably the main reason is that abandoned errors are no exception, and they have not figured out where else to put.Add a point manually!!

JiosDev
  • 324
  • 3
  • 11
  • The exception breakpoint was working for the same code in the previous version of the Xcode. So, can we achieve the same in this version of Xcode through some other means? Is the exception breakpoint working for any other code error in this updated version of Xcode? Or is this an Xcode bug? – Nilanshu Jaiswal Jan 29 '20 at 04:08