panic capture and throw collapse

A, go language panic error capture 

    Go use language students should always use null pointer error such as panic in a real project, a different type of error in the C ++ try-catch block, the language will always go the current panic has spread to the outermost layer of the stack from the stack error , so a lot will go language of architecture entrance handler to add a bunch of code architecture

1     defer func() {
2         if x := recover(); x != nil {
3             // TODO fix panic
4         }
5     }()

     Here to talk about a few keywords

     defer: register a callback function, when the current stack exit, the stack in the order of registration, last registered defer function execution begins, the internal function generator panic, is a correctable type collapse, so the language would go orderly exit stack, and perform defer function

     recover: Capture panic exception and interrupt the current panic, repair processing, guaranteed not to let a single handler affect the whole program

     The basic method above exception caught presumably familiar with the language of the students can go to understand. But let's go learn some languages ​​can not be repaired throw crashes and crashes

 Two , go Language throw Ben collapse 

      In fact, go-language source code in some places throw some calls, this function will print the corresponding fatal msg, and quit the whole program, because this type of error can not be considered a dynamic language go to repair crash. So this kind of collapse and panic ran different, are not captured by the collapse defer and recover (because they can not repair), give two simple chestnuts

     lock: When using an initialization of the lock, the lock will not just unlock code, throw a crash occurs

1 var lock sync.Mutex
2 lock.Unlock()  // fatal: sync: unlock of unlocked mutex
3 
4 
5 // from go 1.91
6 new := atomic.AddInt32(&m.state, -mutexLocked)
7 if (new+mutexLocked)&mutexLocked == 0 {
8     throw("sync: unlock of unlocked mutex") // post a throw
9 }

 

      map: Familiar go language developers know, go under the multi-use facilities by Ctrip architecture, often there are many variables thread-safe, map is one of the most classic chestnuts, when under concurrent, without the addition of read-write locks on the map in the case of write, read and write operations, will also throw throw collapse

testmap := make([int],1)
for i := 0; i < 1000; i++ {
    go func() {
                for true{
                    testmap[1] = 10 // fatal: sync: curcurent map writes
                }    
    }()
}

 

      It should be noted that such a crash is directly off down the whole process, so when we go online using a language for application development, we must remember to use process management tools like the supervisor, be sure to pull up after a crash, then repair. Otherwise it will produce a large area completely machine downtime.

      Longer need to note is that, go in a process language is often a lot goroutinue at the same time, if the throw Ben collapse occurs, the entire process will be turned off, if the log, you will find countless print the log stack information (all goroutinue log), this time do not work hard on the stack logs as log print out are normal, just log in to see the fatal keywords to find the real problem.

 

Guess you like

Origin www.cnblogs.com/novice-dxx/p/11938681.html