Polyglot CheatSheet - Exceptions
Last Updated: 2021-11-19
C++
C++ does have exceptions, however Google C++ Style Guide does not allow exceptions. (https://google.github.io/styleguide/cppguide.html#Exceptions)
try {
// ...
} catch(string &err) {
// ...
No finally
block.
Java
try {
// ...
} catch(Exception e) {
// ...
} finally {
// ...
}
Throw:
throw new RuntimeException("Something is wrong.");
Rust
Rust does not allow throwing exceptions, instead Rust handles errors through its return type.
match safe_div(1.0, 0.0) {
Ok(v) => { println!("{}", v); },
Err(err) => { println!("{}", err); }
}
Python
Throw
raise ValueError('some error message')
Catch
try:
code_that_may_raise_exception()
except ValueError as err:
print(err.args)
Ruby
- begin/end
- rescue keyword
- ensure
- raise