See also: [[Go - Error Handling]] > [!INFO] Golang has exceptions. > > There used to be a fantastic article analyzing Golang's exception facilities, but I can't for the life of me find it anywhere right now. It broke most exception systems down into two categories: > > 1. A simple version that doesn't track the details and > 2. One that stores everything and allows you to resume execution. > > Weirdly, Go does the latter, but makes it difficult for you to do anything interesting with them. > > However, while looking for that article I stumbled across this really rad one which goes through Golang's calling convention at the assembly level: > > [[The Go low-level calling convention on x86-64]] Having to finish building the rest of the exception system that Golang's developers refuse to sure is annoying. They never actually solved the problem that exceptions are the tool for. So, they still need exceptions. So, they have them. - The `panic` keyword is analogous to `throw` in other languages. Just less friendly. - The `defer` keyword is analogous to `finally` in Java. I honestly kinda like this part of their system, though it has issues. - The `recover` keyword is analogous to `catch` in other languages. However, it is restrictive, requiring some minor hoop-jumping to get it to show what it can do. The limitations don't actually make it not an exception system, they just make it require more boilerplate to use. # References ```cardlink url: https://yourbasic.org/golang/recover-from-panic/ title: "Panics, stack traces and how to recover [best practice]" description: "CODE EXAMPLE A panic stops the normal execution of a goroutine and prints a stack trace to the console. The recover function can be used to catch a panic and regain control." host: yourbasic.org favicon: /res/favicon-16x16.png image: https://yourbasic.org/golang/panic.jpg ``` ```cardlink url: https://evanfarrer.blogspot.com/2012/05/go-programming-language-has-exceptions.html title: "The Go Programming Language Has Exceptions" description: "It seems like every other post I read about Go states that the Go programming language doesn't have exceptions. This is followed by either a..." host: evanfarrer.blogspot.com favicon: https://evanfarrer.blogspot.com/favicon.ico ``` ```cardlink url: http://yosefk.com/blog/error-codes-vs-exceptions-critical-code-vs-typical-code.html title: "Error codes vs exceptions: critical code vs typical code" host: yosefk.com ``` ```cardlink url: https://uberpython.wordpress.com/2012/09/23/why-im-not-leaving-python-for-go/ title: "Why I’m not leaving Python for Go" description: "First of all, Go seems like a great language. It has an excellent tutorial which I joyfully went through and found: Go is Fast. Concurrent by design. Typed (important for JIT and IDE’s) but n…" host: uberpython.wordpress.com favicon: https://s0.wp.com/i/favicon.ico image: https://s0.wp.com/i/blank.jpg ``` ```cardlink url: https://groups.google.com/g/golang-nuts/c/kqGL_2p_VCc title: "Re: [go-nuts] Go Error Handling Sucks" host: groups.google.com favicon: https://ssl.gstatic.com/images/branding/product/1x/groups_32dp.png ``` ```cardlink url: https://go.dev/blog/defer-panic-and-recover title: "Defer, Panic, and Recover - The Go Programming Language" description: "An introduction to the Go's defer, panic, and recover control flow mechanisms." host: go.dev favicon: https://go.dev/images/favicon-gopher.png ``` ```cardlink url: https://web.archive.org/web/20140727052105/http://daovm.net/space/dao/thread/937 title: "Dao now supports Go-style panic/exception handling!" host: web.archive.org ``` ```cardlink url: http://lambda-the-ultimate.org/node/4735 title: "Go-style defer-recover exception handling is better than try-catch style? | Lambda the Ultimate" host: lambda-the-ultimate.org ``` ```cardlink url: https://stackoverflow.com/questions/3413389/panic-recover-in-go-v-s-try-catch-in-other-languages title: "Panic recover in Go v.s. try catch in other languages" description: "I've just read this post about Panic/Recover in Go and I'm not clear on how this differs from try/catch in other mainstream languages." host: stackoverflow.com image: https://cdn.sstatic.net/Sites/stackoverflow/Img/[email protected]?v=73d79a89bded ``` ```cardlink url: https://dave.cheney.net/2014/11/04/error-handling-vs-exceptions-redux title: "Error handling vs. exceptions redux | Dave Cheney" host: dave.cheney.net ``` ```cardlink url: https://dave.cheney.net/2012/01/18/why-go-gets-exceptions-right title: "Why Go gets exceptions right | Dave Cheney" host: dave.cheney.net ``` ```cardlink url: https://dave.cheney.net/2015/01/26/errors-and-exceptions-redux title: "Errors and Exceptions, redux | Dave Cheney" host: dave.cheney.net ```