Recoverable Errors

To Share and +4 nLEARNs

In Rust, errors are handled through two types of errors: recoverable and unrecoverable.
While unrecoverable errors are usually the result of a bug in the program and cause the program to panic, recoverable errors allow the program to gracefully handle the error and continue executing.

In this video, we’ll dive into recoverable errors in Rust and learn how to use the Result type to handle errors in a safe and efficient way.

We’ll cover:
– What are recoverable errors in Rust and how they differ from unrecoverable errors
– The Result type and how it is used to handle recoverable errors
– How to use the match expression to handle different types of errors
– The ? operator and how it can simplify error handling code.

By the end of this video, you’ll have a solid understanding of recoverable errors in Rust and be equipped with the knowledge to handle them in your own Rust programs.

Code and slide deck : https://github.com/timurguvenkaya/near-sc-security-course

Introduction to Error Handling in Rust

Section Overview: In this section, the instructor introduces error handling in Rust and explains the differences between recoverable and unrecoverable errors.

Recoverable vs Unrecoverable Errors

  • Recoverable errors can be handled in some way, allowing the program to continue execution.
  • Unrecoverable errors are those that the program cannot handle or recover from, and they will cause the program to terminate.

Risks of Panics

  • Panics are also known as unrecoverable errors and can lead to data loss or corruption.
  • Panics can cause a whole program crash, which can lead to denial of service.

Importance of Handling Errors

  • It’s important to avoid panic whenever possible and handle recoverable errors in a controlled and predictable way.

Example of Recoverable Error Handling

Section Overview: In this section, the instructor provides an example of how to handle a recoverable error using Rust’s match keyword.

Example Code for Recoverable Error Handling

  • Use match keyword to match on both error and success cases when handling a recoverable error.
  • Use question mark operator inside the main function to propagate the error outside.

More Examples of Error Handling in Rust

Section Overview: In this section, the instructor provides additional examples of how to handle errors using Rust’s match keyword and question mark operator.

Example Code for Using Match Keyword

  • Use match keyword with result type when opening a file.
  • Return file metadata if everything is all right, otherwise print an error message and exit the process.

Example Code for Using Question Mark Operator

  • Use question mark operator to propagate errors outside of a function.
  • Print built-in error message if file cannot be opened.

Conclusion

Section Overview: In this section, the instructor concludes the video by summarizing the key points covered in this section.

  • Error handling is an important aspect of programming in Rust.
  • Recoverable errors can be handled using Rust’s match keyword or question mark operator.
  • Unrecoverable errors, or panics, should be avoided whenever possible.
  • Handling errors in a controlled and predictable way is crucial for ensuring program stability.
Generate comment with AI 2 nL
Scroll to Top