# Background

In the course of software application development, a good engineer will have a read of errors that can be encoutered when the application is ran.

# Lineage

1. Software Development:- Function Declaration – Exceptions – Java

# Code

## Outline

1. Import
• std::error::Error
2. Module
• Declare Constants
• const OPERATOR_DIVIDE:char = ‘/’;
const OPERATOR_DIVIDE_BY_ZERO:char = ‘0’;
const OPERATOR_SUBTRACT:char = ‘-‘;
3. Main Function
• Declare local variables
• let mut number1:i32;
let mut number2:i32;
let mut operator:char;
let mut result:Result<i32, Box<dyn Error>>;
• Variable Types
• i32
• number1
• number2
• Operator
• char
• operator
• Result
• i32, Box<dyn Error>
• i32
• number
• Box<dyn Error>
• Exception
• Invoke Function Calc
• Subtract
• Subtract 8 from 10
• Divide
• Divide 10  by 2
• Divide
• Divide by 10 by 0
• Will raise an exception
4. Main Function
• Declare local variables
• let mut number1:i32;
let mut number2:i32;
let mut operator:char;
let mut result:Result<i32, Box<dyn Error>>;
• Variable Types
• i32
• number1
• number2
• Operator
• char
• operator
• Result
• i32, Box<dyn Error>
• i32
• number
• Box<dyn Error>
• Exception
5. Function – calc
• Declare Function
• ``` calc
(
operator:char
, number1:i32
, number2:i32
)-> Result<i32, Box<dyn Error>>
```
• Declare Variable
• let result:i32;
• Return Statement
• return Ok(result);
• OK
• success
• result
• result of function ( int )

## Code Snippet

```
use std::error::Error;

const OPERATOR_DIVIDE:char = '/';
const OPERATOR_DIVIDE_BY_ZERO:char = '0';
const OPERATOR_SUBTRACT:char = '-';

fn main()
{

let mut number1:i32;
let mut number2:i32;
let mut operator:char;
let mut result:Result<i32, Box<dyn Error>>;

/*
Add 10 + 3 = 13
*/
number1 = 10;
number2 = 3;
result = calc(operator, number1, number2);

println!(
"{:0?} {:1?} {:2?} is {:3?}"
, number1
, operator
, number2
, result
);

/*
Subtract
Add 10 - 8 = 2
*/
number1 = 10;
number2 = 8;
operator = OPERATOR_SUBTRACT;
result = calc(operator, number1, number2);

println!(
"{:0?} {:1?} {:2?} is {:3?}"
, number1
, operator
, number2
, result
);

/*
Divide
Add 10 / 2 = 5
*/
number1 = 10;
number2 = 2;
operator = OPERATOR_DIVIDE;
result = calc(operator, number1, number2);

println!(
"{:0?} {:1?} {:2?} is {:3?}"
, number1
, operator
, number2
, result
);

/*
Divide
*/
number1 = 10;
number2 = 0;
operator = OPERATOR_DIVIDE;
result = calc(operator, number1, number2);

println!(
"{:0?} {:1?} {:2?} is {:3?}"
, number1
, operator
, number2
, result
);

}

fn calc
(
operator:char
, number1:i32
, number2:i32
)-> Result<i32, Box<dyn Error>>
{

//declare variables
let result:i32;

{

result = number1 + number2;

}
//if operator is subtract
else if operator == OPERATOR_SUBTRACT
{

result = number1 - number2;

}
//if operator is divide
else if operator == OPERATOR_DIVIDE
{

result = number1 / number2;

}
//if operator is divide by 0
else if operator == OPERATOR_DIVIDE_BY_ZERO
{

//result = number1 / 0;
result =0;

}
//else set result to zero (0)
else
{

result = 0;

}

//return OK indicator and result
return Ok(result);

}

```

## Output

### Textual

```
10 '+'  3 is Ok( 13)
10 '-'  8 is Ok(  2)
10 '/'  2 is Ok(  5)
thread 'main' panicked at 'attempt to divide by zero', main.rs:121:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

...Program finished with exit code 101
Press ENTER to exit console.

```

# Online Code IDE

## OnlineGDB

1. rustCalcInFunction

# Source Code Control

## GitLab

1. Rust – Exception Handling
• Files
• 01) rustCalcInFunctionExceptionDeclared.rs

# Summary

In this post, did not go too far.

Declared a function that exposes a generic error.

We did not attempt to capture the error within an error handler.

Specifically within a try/catch/finally block.