Translate

Panic in golang

What is panic in Go?

panic Go में एक built-in function है जिसका उपयोग program execution को abruptly stop करने के लिए किया जाता है। panic आमतौर पर तब use किया जाता है जब program एक ऐसी स्थिति में आ जाता है जहां से recovery संभव नहीं होती और program को terminate करना जरूरी होता है। जब panic call होता है, तो program की current function की execution तुरंत रोक दी जाती है, और deferred functions (अगर कोई हो) को execute किया जाता है। इसके बाद program crash हो जाता है और एक stack trace output के साथ terminate हो जाता है।

When to Use panic?

panic का उपयोग सिर्फ उन cases में किया जाना चाहिए जहां error इतनी critical हो कि program को तुरंत terminate करना जरूरी हो:

  • Critical Errors: जैसे कि memory corruption, invalid type assertions, या किसी ऐसी स्थिति में जहां program को continue करना unsafe हो।
  • Library Code: अगर आप कोई ऐसा code लिख रहे हैं जिसे अन्य developers use करेंगे, और उसमें कोई situation arises होती है जहां program को रोकना जरूरी हो, तो आप panic का उपयोग कर सकते हैं।

Basic Example of panic

package main

import "fmt"

func main() {
    fmt.Println("Start of the program")
    
    // Panic with a message
    panic("Something went wrong!")

    fmt.Println("This line will never be executed")
}

Explanation:

  • panic("Something went wrong!"): यह statement program को panic mode में डाल देता है और इसे immediately terminate कर देता है।
  • fmt.Println("This line will never be executed"): यह line कभी execute नहीं होगी क्योंकि panic से पहले program terminate हो जाता है।

Panic with Defer

Go में defer statement का उपयोग किसी function के execution के बाद भी कुछ code को execute करने के लिए किया जाता है, चाहे function normally return करे या panic से exit हो। अगर program में panic होती है, तो भी defer block execute होता है।

Example: Panic with Defer

package main

import "fmt"

func main() {
    defer fmt.Println("This deferred function will be executed even after panic")

    fmt.Println("Start of the program")

    // Panic occurs here
    panic("Unexpected error occurred!")

    fmt.Println("This line will never be executed")
}

Explanation:

  • defer fmt.Println(...): यह deferred function panic होने के बाद भी execute होता है।
  • panic("Unexpected error occurred!"): यह program को panic mode में डालता है और execution रोक देता है।

Stack Trace on Panic

जब panic होती है, तो Go runtime एक stack trace print करता है, जो यह बताता है कि panic कहां और कैसे हुई। यह debugging में बहुत useful होता है।

Example: Panic with Stack Trace

package main

func main() {
    f1()
}

func f1() {
    f2()
}

func f2() {
    panic("Something went terribly wrong!")
}

Explanation:

जब panic function f2() में होती है, तो Go runtime पूरे call stack को trace करता है और उसे output में print करता है। इससे आपको यह पता लगाने में मदद मिलती है कि problem कहां से शुरू हुई थी।

Recovering from Panic

हालांकि panic आमतौर पर program को terminate करता है, Go में आप recover function का उपयोग करके panic से recover कर सकते हैं। recover function केवल उस context में useful होता है जहां panic raised हुई हो, यानी कि defer की गई functions में।

Example: Recovering from Panic

package main

import "fmt"

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from panic:", r)
        }
    }()

    fmt.Println("Start of the program")

    // Panic occurs here
    panic("Something went wrong!")

    fmt.Println("This line will not be executed")
}

Explanation:

  • recover(): यह function panic से raised value को return करता है। अगर panic हुई हो, तो recover उसे handle करता है और program execution को continue रखता है।
  • defer func() { ... }(): यह deferred function panic को catch करता है और उसे handle करता है।

When to Avoid Using panic

  • Normal Error Handling: Errors जो normal operations के दौरान होती हैं, जैसे कि file not found, या network timeout, के लिए panic का इस्तेमाल न करें। इनके लिए errors को return करना और उन्हें handle करना बेहतर तरीका है।
  • Recoverable Situations: उन situations में जहां से recovery संभव हो, panic का उपयोग avoid करना चाहिए।

Summary

  • panic: Program execution को abruptly stop करने के लिए use होता है और एक critical situation में program को terminate करता है।
  • Defer and Panic: defer statement के जरिए आप panic के बाद भी कुछ cleanup operations perform कर सकते हैं।
  • Stack Trace: panic होने पर Go runtime एक stack trace print करता है जो debugging में मदद करता है।
  • Recover: आप recover function का use करके panic से recover कर सकते हैं और program को continue रख सकते हैं।
  • Use Cases: panic का उपयोग सिर्फ critical errors या irrecoverable situations में करें।

No comments: