1

During first execution of the loop in the code snippet,an exception throws.my question is how to continue executing next iteration of the loop when panic happens.like try catch mechanism in java,the loop will continue executing next iteration of the loop.

package main

import (
        "fmt"
)

func main() {
        var arr []int = []int{5, 6, 7, 8, 9}
        fmt.Println(arr)
        for i := 6; i < 10; i++ {
                defer func() {
                        fmt.Println("aaa")
                        if err := recover(); err != nil {
                                fmt.Printf("error is %v\n", err)
                        }
                }()
                arr[i] = i

        }

}
3
  • 1. The execution wont be in the loop after defer execution., 2. You need to set atleast 10 as the capacity of slice arr Commented May 4, 2017 at 17:34
  • 2
    Panics are not exceptions. It is a common mistake for programmers used to other languages to treat them a such, but it doesn't work well at all! If you have a panic in Go, then this usually means you've made a programming mistake that needs to be corrected, rather than "handles" by "catching" the panic (like you tried to do here). Commented May 4, 2017 at 17:34
  • my question is how to continue executing next iteration of the loop when panic happens,thanks.like try catch mechanism in java,the loop will continue executing next iteration of the loop. Commented May 4, 2017 at 23:47

2 Answers 2

4

The issue is that your slice has a length and capacity of 5,

https://play.golang.org/p/7wy91PTPum

and you are trying to add something to the 6th position.

You need to either set a fixed size that you know will hold everything you want to put into it:

var arr [10]int = [10]int{5, 6, 7, 8, 9}

https://play.golang.org/p/GSNDXGt1Jp

Or use append and change

arr[i] = i

to

arr = append(arr, i)

https://play.golang.org/p/kHNsFpcjVx

Sign up to request clarification or add additional context in comments.

2 Comments

my question is how to continue executing next iteration of the loop when panic happens,thanks.like try catch mechanism in java,the loop will continue executing next iteration of the loop.
Go is not Java @witrus. It doesn't really have such a mechanism. The way to deal with this is to make sure the panic doesn't happen, either with the suggestions in this answer, or by guarding the assignment with something like if i > len(arr) {... }.
1

You could wrap all work inside some func, and call defer with recover inside it

package main

import (
        "fmt"
)

func doSmth(arr []int, idx int) {
    defer func() {
        if err := recover(); err != nil {
            fmt.Printf("error is %v\n", err)
        }
    }()
    arr[idx] = idx
}

func main() {
    var arr []int = []int{5, 6, 7, 8, 9}
    fmt.Println(arr)
    for i := 6; i < 10; i++ {
        doSmth(arr, i)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.