{"id":23629690,"url":"https://github.com/vrtgs/rust-try-catch","last_synced_at":"2025-04-07T08:15:43.941Z","repository":{"id":267467764,"uuid":"901360366","full_name":"Vrtgs/rust-try-catch","owner":"Vrtgs","description":"Living hell","archived":false,"fork":false,"pushed_at":"2025-02-21T11:52:10.000Z","size":22,"stargazers_count":52,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T07:04:37.027Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Vrtgs.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-10T14:13:11.000Z","updated_at":"2025-03-22T07:50:43.000Z","dependencies_parsed_at":"2025-03-15T22:09:13.281Z","dependency_job_id":"a0313e05-e821-4b03-acab-bcdfff894fbb","html_url":"https://github.com/Vrtgs/rust-try-catch","commit_stats":null,"previous_names":["vrtgs/rust-try-catch"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vrtgs%2Frust-try-catch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vrtgs%2Frust-try-catch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vrtgs%2Frust-try-catch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vrtgs%2Frust-try-catch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Vrtgs","download_url":"https://codeload.github.com/Vrtgs/rust-try-catch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247615377,"owners_count":20967184,"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":[],"created_at":"2024-12-28T01:16:59.026Z","updated_at":"2025-04-07T08:15:43.893Z","avatar_url":"https://github.com/Vrtgs.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rust Try Catch - Reinventing the nightmare!\n\nWelcome to **`rust-try-catch`**, the crate that dares to bring the spirit of Java's infamous `try-catch-finally` block to Rust's peaceful shores.\nWhy settle for *Result* and *Option* when you can spice up your life with panics, exceptions, and an uncanny resemblance to the debugging hellscape you left behind in your old job?\n\n---\n\n## Features (or \"Sins Against Rustaceans\")\n\n- **Bring Back `throw` and `catch`**: Just when you thought the only `throw`ing and `catch`ing you do was in your high-schools baseball team, we decided to give it a whole new purpose: nightmares.\n- **Panic Recovery**: Because catching panics makes more sense than fixing your code, right?\n- **Generic Exception Handling**: Catch `anything`. Who needs type safety when you can have mystery bugs?\n- **Else Blocks**: Pretend you're writing Python.\n- **Finally Blocks**: Clean up after your disasters. Or don’t. We don’t judge.\n- **Readability? Nope.** It’s not a bug; it’s a *feature*. Embrace spaghetti syntax and callback hell in a language that fought so hard to avoid it.\n\n---\n\n## Why?\n\n### Because Error Handling in Rust is Too Safe\nRust’s Result and Option types are just too perfect.\nDevelopers are practically forced to think about errors and handle them responsibly. Where's the thrill of runtime crashes?\nRust must embrace the unpredictability of try-catch so we can get back to debugging stack traces at 2 AM, like real programmers.\n\n### We Miss Nested Pyramid Code\nWithout try-catch, we’re stuck writing linear, readable error handling with .and_then() and the ? operator. UGH Gross!\nWe want to go back to deeply nested code where `try { try { try { ... } catch { ... } } catch { ... } }` creates a delightful maze for future maintainers to explore.\n\n### The World Needs More `catch exception (e)` Jokes\nIn the try-catch world, you can slap a generic `catch exception (e)` block everywhere and call it \"handling errors.\"\nIt’s a rite of passage to spend hours debugging only to realize `catch` swallowed an error you never logged.\nRust users deserve the same rite, don’t you think?\n\n### Your life is already hard enough\nThe `unwrap()` method is far too stigmatized in Rust.\nWhat if we could just shove all possible errors into a catch block and forget about them?\nProductivity will skyrocket when developers no longer worry about pesky things like whether their program works correctly.\n\n### Rust Needs Drama\nrust-try-catch would introduce exciting debates about whether to use exceptions or results.\nThese debates could rival the classic apples vs. oranges.\nRust forums and Reddit threads are way too focused on productive discussions right now. Where’s the chaos?\n\n---\n\n## Getting Started (Warning: Regret Ahead)\n\n### Add to `Cargo.toml`\n\n```toml\n[dependencies]\nrust-try-catch = \"0.1.0\" # Or whatever version the world has suffered through.\n```\n\n---\n\n# Usage: The Gift That Keeps on Giving\n\n\n## `try_catch!` Macro\nAt its core, `try_catch!` is a macro designed to take Rust's Result-based error handling\nand replace it with a beautifully convoluted system of try, catch, else, finally, and questionable life choices.\n\n### Syntax\n```text\ntry_catch! {\n    try {\n        // Your innocent code goes here.\n    } catch (exception =\u003e Type) {\n        // Caught an exception? Great. Now figure out what to do with it.\n    } catch exception (name) {\n        // Catch all other exceptions not previously caught.\n    } catch panic (panic_info) {\n        // For when you're feeling nostalgic about debugging segfaults.\n    } else (res) {\n        // Hooray! Nothing bad happened (yet).\n    } finally {\n        // Clean up whatever mess you've created.\n    }\n}\n```\n\n#### Explanation of Blocks:\n- `try`: Contains the code you're actually trying to run.\n- `catch (exception =\u003e Type)`: Specifically handles exceptions of type Type. Use this for precise error matching.\n- `catch exception (name)`: A catch-all for any exceptions that aren't caught by specific type matches. It's like the last line of defense.\n- `catch panic (panic_info)`: Handles panics (because, why not?).\n- `else`: Runs if the try block finished normally, but before the finally block.\n- `finally`: Ensures some cleanup happens, even if all else fails.\n\n### Example (You Asked for It)\n\n```\nuse rust_try_catch::try_catch;\n\nlet result = try_catch! {\n    try {\n        panic!(\"Oops!\"); // Because Rust's strictness is for cowards.\n    } catch panic (info) {\n        println!(\"Panic caught: {:?}\", info);\n        -42 // Return values that make sense, or don't. We don't care.\n    }\n};\nassert_eq!(result, -42);\n```\n\n---\n\n## `tri!` Macro\n\nThe little sibling of try_catch!, tri! unwraps a Result and throws an exception if it’s an Err.\nIt’s like ?, but with extra regret.\n\n### Example\n\n```\nuse rust_try_catch::{try_catch, tri};\n\nlet result = try_catch! {\n    try {\n        tri!(Err::\u003ci32, \u0026str\u003e(\"Something went wrong\"))\n    } catch (e =\u003e \u0026str) {\n        println!(\"Handled error: {}\", e);\n        -1\n    }\n};\nassert_eq!(result, -1);\n```\n\n---\n\n## `throw` Function\n\nWhen you’re tired of meaningful error propagation, just throw an exception instead.\nBonus points if you hide it under 20 layers of function calls.\n\n### Example\n\n```should_panic\nuse rust_try_catch::throw;\n\nthrow(\"Goodbye, world!\"); // It's not just a panic; it's an *exceptional* panic.\n```\n\n---\n\n## `throw_guard` and `closure_throw_guard`\n\nWhen you need to keep your code from completely imploding due to an unchecked throw, `throw_guard` and `closure_throw_guard` are here to wrap functions or closures in a layer of protection—or at least an illusion of it.\n\nThe `throw_guard` procedural macro can be applied to functions to ensure that thrown exceptions do not escape their bounds.\nUse this when you're feeling particularly responsible.\n\nThis works with async functions too\n\n### Example\n```should_panic\nuse rust_try_catch::throw_guard;\n\n#[throw_guard]\nfn risky_function() {\n    rust_try_catch::throw(\"Oops, I did it again!\");\n}\n\nfn main() {\n    // Instead of crashing, exceptions from risky_function are turned into panics.\n    risky_function();\n}\n```\n\n\nThe `closure_throw_guard` macro wraps closures instead of functions. Perfect for when you want your lambda to crash gracefully.\n### Example\n```should_panic\nuse rust_try_catch::closure_throw_guard;\n\nlet guarded_closure = closure_throw_guard!(|| {\n    rust_try_catch::throw(\"Not today!\");\n});\n\nguarded_closure(); // Exception caught and re-thrown as a panic.\n```\n\n---\n\n# Advanced: Layering Bad Decisions\n\nWhat happens when you combine specific catches,\ngeneric exception handling, panic recovery, and a finally block? Chaos.\n\n```\nuse rust_try_catch::try_catch;\n\nlet result = try_catch! {\n    try {\n        panic!(\"Let's see what happens!\");\n    } catch (e =\u003e \u0026'static str) {\n        println!(\"Caught a static str: {}\", e);\n        0\n    } catch exception (ex) {\n        println!(\"Caught a generic exception: {:?}\", ex);\n        1\n    } catch panic (info) {\n        println!(\"Caught a panic: {:?}\", info);\n        -1\n    } else {\n        println!(\"Didn't catch anything but a cold.\");\n        100\n    } finally {\n        println!(\"Because even bad code needs cleanup.\");\n    }\n};\n\nassert_eq!(result, -1);\n```\n\n# Why use this crate?\n1. You’re feeling nostalgic for Java.\n2. You’re bored and like chaos.\n3. Rust's error handling is too predictable.\n4. Your bug tracker is feeling lonely.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvrtgs%2Frust-try-catch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvrtgs%2Frust-try-catch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvrtgs%2Frust-try-catch/lists"}