3 ms·
There are many ways to have both enforced error handling AND less boilerplate. Java's checked exceptions are much maligned but would work very well here. Anothe
by Sharlin 10y ago
There are many ways to have both enforced error handling AND less boilerplate. Java's checked exceptions are much maligned but would work very well here. Another way would be having more syntactic sugar for Result-style monadic error handling, like the do notation in Haskell or for..yield in Scala.
Another issue raised by the original post is the fact that Rust has no top-level concrete error type that is convertible from all of the specific error types. This is also something that could be fixed without compromising other qualities of the Rust type system.
- burntsushi 10y ago> Another way would be having more syntactic sugar for Result-style monadic error handling [...] Another issue raised by the original post is the fact that Rust has no top-level concrete error type that is convertible from all of the specific error types. Well... Rust has both. Any type that satisfies the `Error` trait can be converted to `Box<Error>`, which is provided by the `impl<T: Error> Error for Box<T>` and `impl<E: Error> From<E> for Box<Error>` impls. And it so happens that this conversion can be done for you automatically using Rust's `?`. For example, this: use std::error::Error; use std::fs::File; use std::io::{self, Read}; fn main() { match example_explicit() { Ok(data) => println!("{}", data), Err(err) => println!("{}", err), } } fn example_explicit() -> Result<String, Box<Error>> { let file = match File::open("/foo/bar/baz") { Ok(file) => file, Err(err) => return Err(From::from(err)), }; let mut rdr = io::BufReader::new(file); let mut data = String::new(); if let Err(err) = rdr.read_to_string(&mut data) { return Err(From::from(err)); } Ok(data) } can be written as this without any fuss: use std::error::Error; use std::fs::File; use std::io::{self, Read}; fn main() { match example_sugar() { Ok(data) => println!("{}", data), Err(err) => println!("{}", err), } } fn example_sugar() -> Result<String, Box<Error>> { let file = File::open("/foo/bar/baz")?; let mut rdr = io::BufReader::new(file); let mut data = String::new(); rdr.read_to_string(&mut data)?; Ok(data) } The problem is that the conversion to `Box<Error>` winds up making it harder for callers to inspect the underlying error if they want to. This is why this approach doesn't work well in libraries, but for simple CLI applications, it's Just Fine.
- kstenerud 10y agoJava's checked exceptions were a disaster. It essentially handcuffed you, limiting what you could do in an overridden method (because you can't add more exceptions to the throws list). So you end up wrapping in RuntimeExceptions and then later having the whole app fall over because the framework that's expecting your class was only designed to handle the checked exceptions. So yeah, want your implementation to consult a database? Sorry, the interface you must implement doesn't declare any checked exceptions, so say hello to app-killing RuntimeException wrapped SQLExceptions :/
- specialist 10y agoChecked exceptions are the correct answer. Just handle it. Exception obfuscation frameworks (Spring) just kick the can down the road, creating problems without any discernible benefit.
- skybrian 10y agoChecked exceptions probably would have been fine if there were only one kind of checked exception. Most methods would declare it and they'd all be compatible. This would be similar to how Go functions always return the same error type, but without the boilerplate.
- bshanks 10y agoBut wouldn't that kind of defeat the point? Sort of like how having a type system with only one type wouldn't be very helpful.
- concede_pluto 10y ago> interface you must implement doesn't declare any checked exceptions It verges on malpractice to design an interface and declare that no implementation of it could possibly ever fail. I'm looking at you, Runnable.
- scott_s 10y agoI ran into this when writing an ANTLR parser. I wanted to throw specialized exceptions based on what went wrong in the parse, but these couldn't be checked exceptions for the above reason. So I had to derive them from RuntimeException, and then I wrapped the call to the parser so that it caught just this kind of exception, and re-threw it as a non-RuntimeException so that it was checked throughout the rest of the code. I think this is an ANTLR-idiom, which I found to be an odd quirk - but one that's unavoidable because of how ANTLR and Java are designed.