Pure Programmer
Blue Matrix


Cluster Map

Exception Handling

L1

This page is under construction. Please come back later.

Many functions take in arguments, do a computation and return a result without any diffuculties. But some functions may run into problems that need to be reported to the calling code. Problems can be found in the arguments being passed, with system resources that are not available or with computations that just can't be performed. Returning an error code through the formal return value of a function is one technique but it requires the calling code to check the result of a function to see if the error code was returned. Often this step is omitted resulting in bad values being propogated through the code. A better approach is to have the function throw an exception. An exception is just an object that is returned through the alternate exceptional return mechanism instead of the normal return mechanism. In the following example the stoi() and stof() functions could fail if the string passed can't be converted to a number. In the case of stoi() this function returns numbers across the whole range of integers so there is no value that can be returned to indicate an error that is not also a valid integer. This is why these two functions return a value if they can and if they can not, they throw an exception. Because the code below doesn't check for execptions being thrown, it will simply quit and print an error message detailing the exception thrown if bad input is passed to either of the two functions.

Exceptions1.rs
fn main() {
	let mut i:isize = 0;
	let mut d:f64 = 0.0;
	i = stoi(args[1])?;
	d = stod(args[2])?;
	println!("{}", format!("i + d = {0:f}", (i) as f64 + d));
}

Output
$ rustc Exceptions1.rs error: unknown format trait `f` --> Exceptions1.rs:6:37 | 6 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions1.rs:4:11 | 4 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions1.rs:5:11 | 5 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `stoi` in this scope --> Exceptions1.rs:4:6 | 4 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions1.rs:5:6 | 5 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0425`. $ rustc Exceptions1.rs error: unknown format trait `f` --> Exceptions1.rs:6:37 | 6 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions1.rs:4:11 | 4 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions1.rs:5:11 | 5 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `stoi` in this scope --> Exceptions1.rs:4:6 | 4 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions1.rs:5:6 | 5 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0425`. $ rustc Exceptions1.rs error: unknown format trait `f` --> Exceptions1.rs:6:37 | 6 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions1.rs:4:11 | 4 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions1.rs:5:11 | 5 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `stoi` in this scope --> Exceptions1.rs:4:6 | 4 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions1.rs:5:6 | 5 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0425`.

In the example below we add exception handling code around each of the two functions that can throw. This is done useing a try/catch construct. The try block wraps the code that might throw an exception. Following the try block is a catch block that contains the error handling code that is only executed if an exception is thrown in the preceding try block. When an exception is thrown, the try block is terminated immediately and control passes to the catch block. If no exception is thrown, all the code in the try block is executed and the catch block is skipped. The example below illustrates what will happen with good input and some bad input.

Exceptions2.rs
use std::error;
use std::sync::Arc;

fn main() {
	let mut i:isize = 0;
	let mut d:f64 = 0;
	match (|| -> Result<(), Arc<dyn error::Error>>{
		i = stoi(args[1])?;
		return Ok(());
	})() {
		Ok(()) => {},
		Err(ex) => {
			{
				println!("Can't convert to integer!");
				exit(1);
			}
		}
	};
	match (|| -> Result<(), Arc<dyn error::Error>>{
		d = stod(args[2])?;
		return Ok(());
	})() {
		Ok(()) => {},
		Err(ex) => {
			{
				println!("Can't convert to floating point!");
				exit(1);
			}
		}
	};
	println!("{}", format!("i + d = {0:f}", (i) as f64 + d));
}

Output
$ rustc Exceptions2.rs error: unknown format trait `f` --> Exceptions2.rs:31:37 | 31 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions2.rs:8:12 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> Exceptions2.rs:15:5 | 15 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::process::exit; | error[E0425]: cannot find value `args` in this scope --> Exceptions2.rs:20:12 | 20 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> Exceptions2.rs:27:5 | 27 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::process::exit; | error[E0308]: mismatched types --> Exceptions2.rs:6:18 | 6 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions2.rs:8:7 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions2.rs:20:7 | 20 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 8 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions2.rs error: unknown format trait `f` --> Exceptions2.rs:31:37 | 31 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions2.rs:8:12 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> Exceptions2.rs:15:5 | 15 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::process::exit; | error[E0425]: cannot find value `args` in this scope --> Exceptions2.rs:20:12 | 20 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> Exceptions2.rs:27:5 | 27 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::process::exit; | error[E0308]: mismatched types --> Exceptions2.rs:6:18 | 6 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions2.rs:8:7 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions2.rs:20:7 | 20 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 8 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions2.rs error: unknown format trait `f` --> Exceptions2.rs:31:37 | 31 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions2.rs:8:12 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> Exceptions2.rs:15:5 | 15 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::process::exit; | error[E0425]: cannot find value `args` in this scope --> Exceptions2.rs:20:12 | 20 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> Exceptions2.rs:27:5 | 27 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::process::exit; | error[E0308]: mismatched types --> Exceptions2.rs:6:18 | 6 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions2.rs:8:7 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions2.rs:20:7 | 20 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 8 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`.

As this code illustrates, exception handling allows use to possibly recover from an error or at a minimum, present a more meaningful error message to the user and then terminate the program in an orderly fashion.

The example above illustrates one way to deal with exceptions, namely providing a try/catch construct to wrap each and every function that might throw. While this a very fine-grained approach that gives us lots of control, it can become tedious if many functions that can throw are being used. Another approach is presented below whereby all the core logic of our main code is wrapped in a single try/catch. If any of the functions in main throw an exception it will cause control flow to immediately jump into the catch handler. This approach requires less exception handling code but sometimes prevents us from knowing exactly which function had a problem that required it to throw an exception.

Exceptions3.rs
use std::error;
use std::sync::Arc;

fn main() {
	let mut i:isize = 0;
	let mut d:f64 = 0;
	match (|| -> Result<(), Arc<dyn error::Error>>{
		i = stoi(args[1])?;
		d = stod(args[2])?;
		println!("{}", format!("i + d = {0:f}", (i) as f64 + d));
		return Ok(());
	})() {
		Ok(()) => {},
		Err(ex) => {
			{
				println!("Can't convert command line argument!");
			}
		}
	};
}

Output
$ rustc Exceptions3.rs error: unknown format trait `f` --> Exceptions3.rs:10:38 | 10 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions3.rs:8:12 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions3.rs:9:12 | 9 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0308]: mismatched types --> Exceptions3.rs:6:18 | 6 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions3.rs:8:7 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions3.rs:9:7 | 9 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 6 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions3.rs error: unknown format trait `f` --> Exceptions3.rs:10:38 | 10 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions3.rs:8:12 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions3.rs:9:12 | 9 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0308]: mismatched types --> Exceptions3.rs:6:18 | 6 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions3.rs:8:7 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions3.rs:9:7 | 9 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 6 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions3.rs error: unknown format trait `f` --> Exceptions3.rs:10:38 | 10 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions3.rs:8:12 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions3.rs:9:12 | 9 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 1 + use std::env::args; | error[E0308]: mismatched types --> Exceptions3.rs:6:18 | 6 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions3.rs:8:7 | 8 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions3.rs:9:7 | 9 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 6 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`.
Exceptions4.rs
mod utils;
use std::error;
use std::sync::Arc;

fn main() {
	let mut i:isize = 0;
	let mut d:f64 = 0;
	match (|| -> Result<(), Arc<dyn error::Error>>{
		i = stoi(args[1])?;
		d = stod(args[2])?;
		println!("{}", format!("i + d = {0:f}", (i) as f64 + d));
		return Ok(());
	})() {
		Ok(()) => {},
		Err(ex) => {
			if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){
				println!("Can't convert command line argument!");
			} else {
				println!("Don't know what went wrong!");
			}
		}
	};
}

Output
$ rustc Exceptions4.rs error: unknown format trait `f` --> Exceptions4.rs:11:38 | 11 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions4.rs:9:12 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions4.rs:10:12 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `ex_` in this scope --> Exceptions4.rs:16:48 | 16 | if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){ | ^^^ help: a local variable with a similar name exists: `ex` error[E0308]: mismatched types --> Exceptions4.rs:7:18 | 7 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions4.rs:9:7 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions4.rs:10:7 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 7 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions4.rs error: unknown format trait `f` --> Exceptions4.rs:11:38 | 11 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions4.rs:9:12 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions4.rs:10:12 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `ex_` in this scope --> Exceptions4.rs:16:48 | 16 | if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){ | ^^^ help: a local variable with a similar name exists: `ex` error[E0308]: mismatched types --> Exceptions4.rs:7:18 | 7 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions4.rs:9:7 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions4.rs:10:7 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 7 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions4.rs error: unknown format trait `f` --> Exceptions4.rs:11:38 | 11 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions4.rs:9:12 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions4.rs:10:12 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `ex_` in this scope --> Exceptions4.rs:16:48 | 16 | if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){ | ^^^ help: a local variable with a similar name exists: `ex` error[E0308]: mismatched types --> Exceptions4.rs:7:18 | 7 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions4.rs:9:7 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions4.rs:10:7 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 7 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`.
Exceptions5.rs
mod utils;
use std::error;
use std::sync::Arc;

fn main() {
	let mut i:isize = 0;
	let mut d:f64 = 0;
	match (|| -> Result<(), Arc<dyn error::Error>>{
		i = stoi(args[1])?;
		d = stod(args[2])?;
		println!("{}", format!("i + d = {0:f}", (i) as f64 + d));
		return Ok(());
	})() {
		Ok(()) => {},
		Err(ex) => {
			if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){
				println!("Can't convert command line argument!");
			} else {
				println!("Don't know what went wrong!");
			}
		}
	};
	// Rust doesn't support true finally blocks
	{
		println!("This statement always executes.");
	}
}

Output
$ rustc Exceptions5.rs error: unknown format trait `f` --> Exceptions5.rs:11:38 | 11 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions5.rs:9:12 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions5.rs:10:12 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `ex_` in this scope --> Exceptions5.rs:16:48 | 16 | if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){ | ^^^ help: a local variable with a similar name exists: `ex` error[E0308]: mismatched types --> Exceptions5.rs:7:18 | 7 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions5.rs:9:7 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions5.rs:10:7 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 7 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions5.rs error: unknown format trait `f` --> Exceptions5.rs:11:38 | 11 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions5.rs:9:12 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions5.rs:10:12 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `ex_` in this scope --> Exceptions5.rs:16:48 | 16 | if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){ | ^^^ help: a local variable with a similar name exists: `ex` error[E0308]: mismatched types --> Exceptions5.rs:7:18 | 7 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions5.rs:9:7 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions5.rs:10:7 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 7 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`. $ rustc Exceptions5.rs error: unknown format trait `f` --> Exceptions5.rs:11:38 | 11 | println!("{}", format!("i + d = {0:f}", (i) as f64 + d)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> Exceptions5.rs:9:12 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `args` in this scope --> Exceptions5.rs:10:12 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope | help: consider importing this function | 2 + use std::env::args; | error[E0425]: cannot find value `ex_` in this scope --> Exceptions5.rs:16:48 | 16 | if let Some(ex_) = utils::isCustomErrorType(ex_.clone(), utils::CustomError::NumberFormatError("".to_string())){ | ^^^ help: a local variable with a similar name exists: `ex` error[E0308]: mismatched types --> Exceptions5.rs:7:18 | 7 | let mut d:f64 = 0; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `0.0` | expected due to this error[E0425]: cannot find function `stoi` in this scope --> Exceptions5.rs:9:7 | 9 | i = stoi(args[1])?; | ^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> Exceptions5.rs:10:7 | 10 | d = stod(args[2])?; | ^^^^ not found in this scope error: aborting due to 7 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`.

Questions

Projects

More ★'s indicate higher difficulty level.

References