Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -1,5 +1,5 @@
class MyException : Exception
{
constructor new()
<= new:"MyException raised";
<= new("MyException raised");
}

View file

@ -2,7 +2,7 @@ try
{
o.foo()
}
catch(MyException e)
catch:(MyException e)
{
// handle exceptions of type MyException and derived
}

View file

@ -0,0 +1,51 @@
// IO error is used here just as an example of an already existing
// Error
use std::io::{Error, ErrorKind};
// Rust technically doesn't have exception, but different
// types of error handling. Here are two examples of results.
fn valid_function() -> Result<usize, Error> {
Ok(100)
}
fn errored_function() -> Result<usize, Error> {
Err(Error::new(ErrorKind::Other, "Something wrong happened."))
}
// This should happen only when an unrecoverable error happened
fn panicking_function() {
panic!("Unrecoverable state reached");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_valid_function() {
let result = match valid_function() {
Ok(number) => number,
Err(_) => panic!("This is not going to happen"),
};
assert_eq!(result, 100);
}
#[test]
fn test_errored_function() {
let result = match errored_function() {
Ok(_) => panic!("This is not going to happen"),
Err(e) => {
assert_eq!(e.to_string(), "Something wrong happened.");
0
}
};
assert_eq!(result, 0);
}
#[test]
#[should_panic]
fn test_panicking_function() {
panicking_function();
}
}