-
Notifications
You must be signed in to change notification settings - Fork 5
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
Labels
Comments
Quick example here to show the usage of 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 |
for the record: Here are minimalist examples of 1. Pythontry:
1 / 0
except ZeroDivisionError:
print("Error: Division by zero")
raise ValueError("Custom error") 2. Javatry {
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. JavaScripttry {
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. Gopackage 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. Rubybegin
raise "Custom error"
rescue => e
puts "Caught: #{e.message}"
raise
end 8. Swiftenum MyError: Error {
case customError
}
do {
throw MyError.customError
} catch {
print("Caught:", error)
throw error
} 9. Kotlinfun main() {
try {
throw Exception("Custom error")
} catch (e: Exception) {
println("Caught: ${e.message}")
throw e
}
} 10. Rustfn 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 ( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: