{"id":13412770,"url":"https://github.com/rbrahul/exception","last_synced_at":"2025-04-15T01:27:32.660Z","repository":{"id":37757328,"uuid":"492367345","full_name":"rbrahul/exception","owner":"rbrahul","description":"A simple utility package for exception handling with try-catch in Golang","archived":false,"fork":false,"pushed_at":"2022-11-21T04:49:30.000Z","size":108,"stargazers_count":29,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-07-31T20:51:24.067Z","etag":null,"topics":["exception-handling","go","golang","golang-library","golang-package","try-catch"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rbrahul.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-15T02:16:49.000Z","updated_at":"2024-07-31T18:45:21.000Z","dependencies_parsed_at":"2022-08-08T21:31:08.745Z","dependency_job_id":null,"html_url":"https://github.com/rbrahul/exception","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbrahul%2Fexception","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbrahul%2Fexception/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbrahul%2Fexception/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbrahul%2Fexception/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rbrahul","download_url":"https://codeload.github.com/rbrahul/exception/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248987881,"owners_count":21194313,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exception-handling","go","golang","golang-library","golang-package","try-catch"],"created_at":"2024-07-30T20:01:28.993Z","updated_at":"2025-04-15T01:27:32.643Z","avatar_url":"https://github.com/rbrahul.png","language":"Go","readme":"![Exception](gopher.png)\n\n![Go test workflow](https://github.com/rbrahul/exception/actions/workflows/go.yaml/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/rbrahul/exception)](https://goreportcard.com/report/github.com/rbrahul/exception)\n[![codecov](https://codecov.io/gh/rbrahul/exception/branch/main/graph/badge.svg?token=CW54A6HWS6)](https://codecov.io/gh/rbrahul/exception)\n[![Go Reference](https://pkg.go.dev/badge/github.com/rbrahul/exception.svg)](https://pkg.go.dev/github.com/rbrahul/exception)\n\n## Go Try Catch Exception Handler\nBy design, Go doesn't offer any mechanism for Exception handling. But Programmers from different backgrounds like Java, C++, Php can be sceptical about the decision. Exception handling with *Try Catch Finally* is well adapted in all the modern languages. To ease the pain, this library offers utility functions for Exception Handling, which will help programmers to write Go code with *Try-Catch-Finally* approach.\n\n### This is how you can throw Exception and handle within Catch:\n\n```go\n import(\n     e \"github.com/rbrahul/exception\"\n )\n\n...\n    e.Try(func() {\n        data := getValue() // get me the value from Allions\n        if data != 100 {\n\t\t    e.Throw(e.AssertionError(\"Expected value is not same as 100\"))\n        }\n\t})\n    .Catch(e.In(e.AssertionErrorType, e.ValueErrorType), func(excep *Exception) {\n        fmt.Println(\"Message:\",excep.Message)\n        fmt.Println(\"Exception Type:\",excep.Type)\n        fmt.Println(\"Here is the Stack Trace:\",excep.StackTrace)\n    })\n    .Catch(nil, func(excep *Exception) {\n        fmt.Println(\"I'll be executed as fallback:\",excep.Message)\n    })\n    .Finally(func() {\n\t\tfmt.Println(\"I have been executing always to clean the world!\")\n\t})\n    .Run()\n...\n```\n\n### Throwing a custom exception\n\nYou have to define a exception variable with ExceptionType.\n\n```go\nconst SomethingWentWrongError  e.ExceptionType = \"SomethingWentWrongError\"\n```\n\nNow you have to initialize and throw your exception via e.New constructor. You can pass a proper error message as optional argument.\n\n```go\n    e.Try(func() {\n        e.Throw(e.New(SomethingWentWrongError, \"Something went worng!\"))\n\t})\n    .Catch(e.In(SomethingWentWrongError), func(excep *Exception) {\n        fmt.Println(\"Message:\",excep.Message)\n        fmt.Println(\"Exception Type:\",excep.Type)\n    })\n    .Finally(func() {\n\t\tfmt.Println(\"I'm Gonna fix it!\")\n\t})\n    .Run()\n```\n\n### You can wrap any panic with try-catch and recover it elegently\n\n```go\n    e.Try(func() {\n        panic(\"I'm gonna panic but don't worry\")\n\t})\n    .Catch(nil, func(excep *Exception) {\n        fmt.Println(\"I knew you are gonna catch me :p\", excep.Message)\n    })\n    .Run()\n```\n","funding_links":[],"categories":["Error Handling","错误处理"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frbrahul%2Fexception","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frbrahul%2Fexception","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frbrahul%2Fexception/lists"}