func errorFunction()
{
do
{
let arrayOfInts: [Int] = [1, 2, 3]
let i:Int = arrayOfInts[5]
print(i)
}
catch
{
print(error)
}
}
At the line where catch
is written, it gives me warning 'catch' block is unreachable because no errors are thrown in 'do' block
. However, as you can see, the code is going to throw Index out of bounds
error
. Also, when I run this code, the code truly does not reach catch
block.
How can I catch this error?
Please note that I do not want to catch this exception. Using array was just an example to demonstrate. The reason for asking this question is to understand how to use this catch
block to catch this or similar exceptions.
PS
- If you find my question too naive, please feel free to down-vote - but do give an answer for it - I'm really looking for one.