Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -1,5 +1,5 @@
|
|||
class MyException : Exception
|
||||
{
|
||||
constructor new()
|
||||
<= new:"MyException raised";
|
||||
<= new("MyException raised");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ try
|
|||
{
|
||||
o.foo()
|
||||
}
|
||||
catch(MyException e)
|
||||
catch:(MyException e)
|
||||
{
|
||||
// handle exceptions of type MyException and derived
|
||||
}
|
||||
|
|
|
|||
51
Task/Exceptions/Rust/exceptions.rust
Normal file
51
Task/Exceptions/Rust/exceptions.rust
Normal 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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue