Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThrowStmt - Represents a throw statement (for exceptions). #100

Closed
Tracked by #73
xmnlab opened this issue Sep 17, 2024 · 2 comments · Fixed by #179
Closed
Tracked by #73

ThrowStmt - Represents a throw statement (for exceptions). #100

xmnlab opened this issue Sep 17, 2024 · 2 comments · Fixed by #179
Assignees
Labels

Comments

@xmnlab
Copy link
Contributor

xmnlab commented Sep 17, 2024

No description provided.

@apkrelling
Copy link
Contributor

apkrelling commented Feb 13, 2025

Quick example here to show the usage of try, raise, except, finally in Python:

def divide(a, b):
    try:
        if b == 0:
            raise ValueError("Cannot divide by zero!")  # Raising an exception for zero division
        result = a / b
    except ValueError as e:
        print(f"Error: {e}")
        result = None
    finally:
        print("Execution finished.")  # This runs no matter what
    return result

# Example usage
print(divide(10, 2))  # Normal case
print(divide(10, 0))  # Raising an exception

@xmnlab
Copy link
Contributor Author

xmnlab commented Feb 14, 2025

for the record:

Here are minimalist examples of try/except (or catch) and throw (or raise) in 10 popular programming languages:

1. Python

try:
    1 / 0
except ZeroDivisionError:
    print("Error: Division by zero")
    raise ValueError("Custom error")

2. Java

try {
    int result = 1 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: Division by zero");
    throw new RuntimeException("Custom error");
}

3. C++

#include <iostream>
using namespace std;

int main() {
    try {
        throw runtime_error("Custom error");
    } catch (const runtime_error &e) {
        cout << "Caught: " << e.what() << endl;
        throw;
    }
}

4. JavaScript

try {
    throw new Error("Custom error");
} catch (error) {
    console.error("Caught:", error.message);
    throw error;
}

5. C#

try {
    throw new Exception("Custom error");
} catch (Exception e) {
    Console.WriteLine("Caught: " + e.Message);
    throw;
}

6. Go

package main
import "fmt"

func risky() error {
    return fmt.Errorf("custom error")
}

func main() {
    if err := risky(); err != nil {
        fmt.Println("Caught:", err)
        panic(err)
    }
}

7. Ruby

begin
  raise "Custom error"
rescue => e
  puts "Caught: #{e.message}"
  raise
end

8. Swift

enum MyError: Error {
    case customError
}

do {
    throw MyError.customError
} catch {
    print("Caught:", error)
    throw error
}

9. Kotlin

fun main() {
    try {
        throw Exception("Custom error")
    } catch (e: Exception) {
        println("Caught: ${e.message}")
        throw e
    }
}

10. Rust

fn risky() -> Result<(), &'static str> {
    Err("Custom error")
}

fn main() {
    match risky() {
        Ok(_) => println!("Success"),
        Err(e) => {
            println!("Caught: {}", e);
            panic!("Throwing error")
        }
    }
}

These examples illustrate basic error handling (try/catch or try/except) and throwing exceptions (throw or raise) across different languages. Let me know if you need any further explanations!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants