If you are coming from Java background, where you have used Exception Handling extensively and you have started working on new language like RUST, which doesn’t support Exception Handling then you will be forced to think that does such kind of world really exists?
The Java
Java provides full coverage to Exceptions and those are required to be handled try-catch
Error
“indicates serious problems that a reasonable application should not try to catch.” and an Exception
“indicates conditions that a reasonable application might want to catch.“. Exceptions are further divided into two types: Checked and Unchecked. Checked exceptions are generally those from which a program can recover for exFileNotFoundException
ClassNotFoundException
try-catch
throws
Unchecked exceptions are not checked at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. Most of the times these exception occurs due to the bad data provided by NullPointerException
, ArrayIndexOutOfBoundsException
.
Errors are also unchecked exception & the programmer is not required to do anything with these. In fact, it is a bad idea to use a try-catch
clause for Errors. Recovering from Error is not possible. We can recover from exceptions by either try-catch
RUST – fresh air
Rust
solves the exception problem by not having exceptions.
Rust doesn’t support throwing exceptions, nor does it support catching them. Instead,
Rust provides two special generic enums:
I) Option<T>
andResult<T, E>
for recoverable errors and
II ) panic!
This strategy is the most appreciated inside the functional programming world.
Rust does not use the concept of null
\ nil
\ undefined
types to represent empty outputs. Option<T>
can have Some(T)
None
fn match_color(color: Option<&str>) {
match color {
Some("Red") => println!("Hey I got Red T-shirt"),
Some(c) => println!("I don't like {} T-shirt", c),
None => println!("No T-shirt"),
}
}
In Result<T,E>
Some(T)
None
Ok(T)
Err(E)
Ok(T)
basically works exactly Some(T)
Err(E)
is a wrapper for a value that represents the error.
fn parse_string_to_int(str: &str) {
let i: Result<i32, ParseIntError> = str.parse::<i32>();
match i {
Ok(t) => println!("Here is parsed integer {}!", t),
Err(e) => panic!("There was a problem {:?}", e),
};
}
In above example, if you send invalid string, you will see below error message.
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
Running `target/debug/hello-rust`
thread 'main' panicked at 'There was a problem ParseIntError { kind: InvalidDigit }', src/main.rs:13:19
note: Run with `RUST_BACKTRACE=1` for a backtrace.
The advantage of Result<T, E>
and Option<T>
is that you know exactly what you are getting.
In Java, It is possible to catch OutOfMemoryError
Explore more about this interesting topic “Live without Exceptions in Rust” in our upcoming webinar on 20th
Reference:
Why Rust’s error handling is awesome

1 thought on “You can live without Exceptions, If you are using RUST3 min read”
Comments are closed.