I have to run a `for`

loop based on random numbers generation and inversion of a matrix. When the matrix is singular, my code stops, throwing a `SingularException()`

error. If this occurs at a certain iteration of the loop, how can I force the code to restart again the iteration with new numbers generation until a regular matrix is found to continue the loop until the end?

To keep it simple, suppose the following code:

```
res=zeros(500)
for i=1:length(res)
X=rand(100,100)
X2=transpose(X)*X
res[i]=tr(inv(X2))
end
```

If at iteration `i`

the matrix `X2`

is singular, how can I run again iteration `i`

until the iteration can be completed and then proceed with the loop?