Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Rust equivalent to a try-catch statement?

Is it possible to handle multiple different errors at once instead of individually in Rust without using additional functions? In short: what is the Rust equivalent to a try-catch statement?

A feature like this (First-class error handling with ? and catch) was suggested back in 2016, but I can't tell what came out of it and how a 2019 solution for such a problem might look like.

For example, doing something like this:

try {     do_step_1()?;     do_step_2()?;     do_step_3()?;     // etc } catch {     alert_user("Failed to perform necessary steps"); } 

Instead of:

match do_steps() {     Ok(_) => (),     _ => alert_user("Failed to perform necessary steps") }  // Additional function: fn do_steps() -> Result<(), Error>{     do_step_1()?;     do_step_2()?;     do_step_3()?;     // etc     Ok(()) } 

My program has a function which checks a variety of different places in the registry for different data values and returns some aggregate data. It would need to use many of these try-cache statements with try-catch inside of other try-catch inside of loops.

like image 216
Marc Guiselin Avatar asked Apr 19 '19 01:04

Marc Guiselin


People also ask

Is there try catch in Rust?

And remember, Rust doesn't have a try... catch , so this could be a pretty nasty issue. Notice we're using an underscore _ inside that Err at the end. This is the Rust-y way of saying, “We don't care about this value,” because we're always returning a status of 400 .

Is try catch the same as if else?

In 'try-catch' the codes to handle the exceptions and what exception to be handled, that are easily readable. In 'if-else', we have one else block corresponding to one if block. Or we need to define another condition with command 'else if'. In 'try-catch' we don't have to define each 'try' block with a 'catch' block.

What is a try catch statement?

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception.

Is try except the same as try catch?

In the try clause, all statements are executed until an exception is encountered. except is used to catch and handle the exception(s) that are encountered in the try clause. else lets you code sections that should run only when no exceptions are encountered in the try clause.


1 Answers

There is no try catch statement in Rust. The closest approach is the ? operator.

However, you do not have to create a function and a match statement to resolve it in the end. You can define a closure in your scope and use ? operator inside the closure. Then throws are held in the closure return value and you can catch this wherever you want like following:

fn main() {     let do_steps = || -> Result<(), MyError> {         do_step_1()?;         do_step_2()?;         do_step_3()?;         Ok(())     };      if let Err(_err) = do_steps() {         println!("Failed to perform necessary steps");     } } 

Playground

Is it possible to handle multiple different errors at once instead of individually in Rust without using additional functions?

There is a anyhow crate for the error management in Rust mostly recommended nowadays.

As an alternative, There is a failure crate for the error management in Rust. Using Failure, you can chain, convert, concatenate the errors. After converting the error types to one common type, you can catch (handle) it easily.

like image 147
Akiner Alkan Avatar answered Sep 21 '22 22:09

Akiner Alkan