• Java IO
  • jQuery
  • RubyonRails
  • JavaScript
  • Struts
  • Servlet
  • PHP
  • JSP
  • SQL Tutorial

  • <

    Rust Error Handling

    Rust does not have exceptions. Rust errors are devided in two major categories:

  • recoverable (Ex: file not found error)
  • unrecoverable errors(Ex:bugs)

  • Rust Recoverable Error Example


    use std::fs::File;
    fn main() {
       let f = File::open("dog.jpg"); 
       //this image file does not exist
       println!("{:?}",f);
    }
    

    Output
    Err(Error { repr: Os { code: 2, message: "No such file or directory" } })
    
    


    Rust Unrecoverable Error Example

    panic! macro used when a program reaches an unrecoverable state.

    fn main() {
       panic!("HelloWorld");
       println!("End of main"); //unreachable statement
    }
    

    Output
    thread 'main' panicked at 'HelloWorld', main.rs:3
    
    
















    © copyright 2017-2021 Completedone pvt ltd.