Last active
December 1, 2018 18:41
-
-
Save corebreaker/6a93c8210425e96dc1bcbb157f0270b0 to your computer and use it in GitHub Desktop.
Even if the syntax seems be different, the try/catch/finally paradigm exists in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Java code: | |
---------- | |
try // <-- TRY HEADER | |
{ | |
throw new Exception("Error"); // <-- TRY BODY (RAISE Exception) | |
} | |
catch(final Exception err) // <-- CATCH HEADER | |
{ | |
handler(err); // <-- CATCH BODY | |
} | |
finally | |
{ | |
end(); // <-- FINALLY BODY | |
} | |
*/ | |
func try_catch_finally_example() { | |
func() { // <-- TRY HEADER | |
defer func() { | |
end() // <-- FINALLY BODY | |
}() | |
defer func() { | |
if err := recover(); err != nil { // <-- CATCH HEADER | |
handler(err) // <-- CATCH BODY | |
} | |
}() | |
panic("Error") // <-- TRY BODY (RAISE Exception) | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment