{"id":1311,"date":"2021-02-22T06:34:17","date_gmt":"2021-02-22T06:34:17","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1311"},"modified":"2021-02-22T06:35:11","modified_gmt":"2021-02-22T06:35:11","slug":"golang-try-catch","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/02\/22\/golang-try-catch\/","title":{"rendered":"GoLang: try-catch"},"content":{"rendered":"<p>GoLang doesn&#8217;t have a try and catch like the popular C++ or Java languages, what it has is <code>panic-recover<\/code>.<\/p>\n\n\n<p>Non critical errors are returned as normal values and for the critical ones, <code>panic<\/code> is used<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func myFunc(myvalue string) (string, error)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Error<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><code>errors.New()<\/code><\/li><li><code>fmt.Errorf()<\/code><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>func throwError() (string, error) {\n\treturn \"\", fmt.Errorf(\"I am an simple error\")\n}\n\nfunc main() {\n\t_, err := throwError()\n\n\tif err != nil {\n\t\tfmt.Println(\" catch error....\" + err.Error())\n\t}\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>catch error....I am an simple error<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Recover<\/h2>\n\n\n\n<p><strong>Recover<\/strong>&nbsp;is a built-in function that allows the regain of control from a panicking goroutine. For the normal flow of execution, a call to recover will return <code>nil<\/code> and will have no effect. <\/p>\n\n\n\n<p>For a panicking <code>gomodule<\/code>, call to recover will capture the value given to panic and <strong>resume normal execution<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote is-style-solid-color\"><blockquote><p><strong>&nbsp;Recover is only useful inside deferred functions.<\/strong><\/p><\/blockquote><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>func throwPanic() {\n\tfmt.Println(\"I will throw panic after few seconds.\")\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tfmt.Printf(\" Caught Panic: %v\\n\", r)\n\t\t}\n\t}()\n\tpanic(fmt.Errorf(\"%s\", \"Panic has struck....\"))\n}\n\nfunc main() {\n\tfmt.Println(\"main function\")\n\tthrowPanic()\n\tfmt.Println(\"I will now print...\")\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>main function\nI will throw panic after few seconds.\n Caught Panic: Panic has struck....\nI will now print...<\/code><\/pre>\n\n\n\n<p>You can now see that in function <code>throwPanic()<\/code> we have defined a <code>deferred<\/code> function invocation in which the recover function will be invoked to see what error has happened and finally the normal flow resumes and hence the &#8220;<code>I will now print<\/code>&#8221; is returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Help<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/blog.golang.org\/defer-panic-and-recover\">https:\/\/blog.golang.org\/defer-panic-and-recover<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GoLang doesn&#8217;t have a try and catch like the popular C++ or Java languages, what it has is panic-recover. Non critical errors are returned as normal values and for the critical ones, panic is used Creating Error errors.New() fmt.Errorf() Recover Recover&nbsp;is a built-in function that allows the regain of control from a panicking goroutine. For [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1328,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[137,34],"tags":[158,23,159,160,157],"class_list":["post-1311","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","category-technical","tag-defer","tag-golang","tag-panic","tag-recover","tag-try-catch","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1311"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1328"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}