loading

Rust Error Handling

Rust Error handling

  • Through the use of an error handling system, Rust can identify potential errors and notify you when necessary before compiling the code.
  • Because you can find and fix mistakes before deploying the code for production, this approach increases the program’s robustness.
  • Rust is a programming language without exceptions.

There are two types of errors in Rust:

  • Unrecoverable error:
  • Recoverable error
Rust Error Handling -

Recoverable Error:: Errors that can be recovered are ones that alert the user so they can try the operation again. Errors that can be corrected don’t necessarily mean the process has to end. Result<T,E> serves as its representation.

“File not found” is an example of a recoverable error.

T-> This kind of value is one that is given back with a “OK” version in a successful scenario.

E-> This type of error is one that has a “Err” variant and is returned in a failure scenario.

Unrecoverable Error: The panic! macro halts program execution when the Rust reports an unrecoverable error. Take “Divide by zero” as an illustration of an unrecoverable blunder.

Recoverable Error vs Unrecoverable Error

An error that is recoverable can be fixed in some fashion, whereas an error that is unrecoverable cannot be fixed at all.

Let's see a scenario of expected behavior:

				
					"100".parse();
				
			

Since “100” in the example above is a string, it is unclear if the example will succeed or fail. This is the anticipated conduct. It is therefore a recoverable error.

Unexpected behavior

Rust Error Handling -

assert: To declare something to be true, use the assert! command. Should the input be insufficiently accurate or inaccurate, the application will terminate the run. If the expression is not evaluated as true at runtime, it raises the panic! error.

Example:

				
					 fn main()  
{  
let x : bool = false;  
assert!(x==true);  
}  
				
			

Output:

Rust Error Handling -

The condition inside the assert! in the example above indicates that the value of x is false. False is macro. Thus, an assertion! Bring on the terror! during the runtime.

inaccessible: An inaccessible The inaccessible code uses macro. Since the compiler is unable to identify unreachable code, this macro is helpful. The inaccessible! at runtime determines what code is unreachable.

Example:

				
					             enum Value  
{  
  Val,  
}  
  
fn get_number(_:Value)->i32  
{   
   5  
}  
fn find_number(val:Value)-> &'static str  
{  
  match get_number(val)  
  {  
    7 => "seven",  
    8=> "eight",  
    _=> unreachable!()  
  }  
}  
  
fn main()  
{  
  println!("{}", find_number(Value::Val));  
}  
				
			

Output:

Rust Error Handling -

In the aforementioned example, the get_number() function returns a result of 5, which matches each pattern but not any of the others. Consequently, the inaccessible! Panic is called by macro! large.

Share this Doc

Rust Error Handling

Or copy link

Explore Topic