You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version)?
1.7.3
What operating system and processor architecture are you using (go env)?
windows/amd64
What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
package main
import (
"fmt""time"
)
/*********TASK************/typeTaskstruct {
channelchanbooldonebool
}
func (task*Task) Run() {
time.AfterFunc(time.Second*5, func() { //this callback will run in another goroutine,not the current onepanic("Manual error")
task.channel<-true
})
<-task.channeltask.done=true
}
func (task*Task) IsDone() bool {
returntask.done
}
funcNewTask() *Task {
t:=new(Task)
t.channel=make(chanbool, 1)
t.done=falsereturnt
}
/************TRANSACTION***************/typeTransactionstruct {
task*Taskis_errbool
}
func (m*Transaction) SetTask(task*Task) {
m.task=task
}
func (m*Transaction) IsError() bool {
returnm.is_err
}
func (m*Transaction) IsDone() bool {
returnm.task.IsDone()
}
func (m*Transaction) Run() {
deferfunc() { //I'd like to catch the error throwed by the task//,no matter if the error is occurred in the current goroutine or goroutine created by task.Run and etc.//Here , I suppose there is a goroutine tree,the error will spread from the function call stack at first in the current goroutine// (can be recovered use defer),and if it is not caught manually in the current gorountine ,it will be spread to the parent.//but ,in fact ,it is wrong. And if it is possible and significative to implement this feature?ifr_c:=recover(); r_c!=nil {
m.is_err=true
}
}()
m.task.Run()
}
funcNewTransaction() *Transaction {
m:=new(Transaction)
m.is_err=falsereturnm
}
funcmain() {
task:=NewTask()
m:=NewTransaction()
m.SetTask(task)
gom.Run()
fmt.Println("Task is running")
for {
ifm.IsDone() ||m.IsError() {
break
}
}
ifm.IsError() {
fmt.Println("Error")
} else {
fmt.Println("done")
}
}
What did you expect to see?
I'd like to caught the error occurred in the child goroutine(There is no child goroutine in Golang).
What did you see instead?
I failed to caught the error. And if it is possible and significative to implement goroutine tree and spread error along the tree?
The text was updated successfully, but these errors were encountered:
mikioh
changed the title
Is it significative and possible to recover(catch) the uncaught error in sub goroutine?
runtime: Is it significative and possible to recover(catch) the uncaught error in sub goroutine?
Oct 30, 2016
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.7.3
What operating system and processor architecture are you using (
go env
)?windows/amd64
What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
What did you expect to see?
I'd like to caught the error occurred in the child goroutine(There is no child goroutine in Golang).
What did you see instead?
I failed to caught the error. And if it is possible and significative to implement goroutine tree and spread error along the tree?
The text was updated successfully, but these errors were encountered: