Skip to content

Instantly share code, notes, and snippets.

@corebreaker
Last active December 1, 2018 18:41
Show Gist options
  • Save corebreaker/6a93c8210425e96dc1bcbb157f0270b0 to your computer and use it in GitHub Desktop.
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
/*
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