Struct Exception

Source
pub struct Exception { /* private fields */ }
Expand description

This struct represents the exceptional results of evaluating a Molt script, as used in MoltResult. It is often used as the Err type for other functions in the Molt API, so that these functions can easily return errors when used in the definition of Molt commands.

A Molt command or script can return a normal result, as indicated by MoltResult’s Ok variant, or it can return one of a number of exceptional results via Err(Exception). Exceptions bubble up the call stack in the usual way until caught. The different kinds of exceptional result are defined by the ResultCode enum. Client code is primarily concerned with ResultCode::Error exceptions; other exceptions are handled by the interpreter and various control structure commands. Except within application-specific control structure code (a rare bird), non-error exceptions can usually be ignored or converted to error exceptions— and the latter is usually done for you by the interpreter anyway.

Implementations§

Source§

impl Exception

Source

pub fn is_error(&self) -> bool

Returns true if the exception is an error exception, and false otherwise. In client code, an Exception almost always will be an error; and unless you’re implementing an application-specific control structure can usually be treated as an error in any event.

§Example

let mut interp = Interp::new();
let input = "throw MYERR \"Error Message\"";

match interp.eval(input) {
   Ok(val) => (),
   Err(exception) => {
       assert!(exception.is_error());
   }
}
Source

pub fn error_code(&self) -> Value

Returns the exception’s error code, only if is_error(). exception.

§Panics

Panics if the exception is not an error.

Source

pub fn error_info(&self) -> Value

Returns the exception’s error info, i.e., the human-readable error stack trace, only if is_error().

§Panics

Panics if the exception is not an error.

Source

pub fn error_data(&self) -> Option<&ErrorData>

Gets the exception’s ErrorData, if any; the error data is available only when the code() is ResultCode::Error. The error data contains the error’s error code and stack trace information.

§Example

let mut interp = Interp::new();
let input = "throw MYERR \"Error Message\"";

match interp.eval(input) {
   Ok(val) => (),
   Err(exception) => {
       if let Some(error_data) = exception.error_data() {
           assert_eq!(error_data.error_code(), "MYERR".into());
       }
   }
}
Source

pub fn code(&self) -> ResultCode

Gets the exception’s result code.

§Example

This example shows catching all of the possible result codes. Except in control structure code, all of these but ResultCode::Return can usually be treated as an error; and the caller of Interp::eval will only see them if the script being called used the return command’s -level option (or the Rust equivalent).


let mut interp = Interp::new();
let input = "throw MYERR \"Error Message\"";

match interp.eval(input) {
   Ok(val) => (),
   Err(exception) => {
       match exception.code() {
           ResultCode::Okay => { println!("Got an okay!") }
           ResultCode::Error => { println!("Got an error!") }
           ResultCode::Return => { println!("Got a return!") }
           ResultCode::Break => { println!("Got a break!")  }
           ResultCode::Continue => { println!("Got a continue!")  }
           ResultCode::Other(n) => { println!("Got an other {}", n)  }
       }
   }
}
Source

pub fn value(&self) -> Value

Gets the exception’s value, i.e., the explicit return value or the error message. In client code, this will almost always be an error message.

§Example

This example shows catching all of the possible result codes. Except in control structure code, all of these but ResultCode::Return can usually be treated as an error; and the caller of Interp::eval will only see them if the script being called used the return command’s -level option (or the Rust equivalent).


let mut interp = Interp::new();
let input = "throw MYERR \"Error Message\"";

match interp.eval(input) {
   Ok(val) => (),
   Err(exception) => {
       assert_eq!(exception.value(), "Error Message".into());
   }
}
Source

pub fn level(&self) -> usize

Gets the exception’s level. The “level” code is set by the return command’s -level option. See The Molt Book’s return page for the semantics. Client code should rarely if ever need to refer to this.

Source

pub fn next_code(&self) -> ResultCode

Gets the exception’s “next” code (when code == ResultCode::Return only). The “next” code is set by the return command’s -code option. See The Molt Book’s return page for the semantics. Client code should rarely if ever need to refer to this.

Source

pub fn add_error_info(&mut self, line: &str)

Adds a line to the exception’s error info, i.e., to its human readable stack trace. This is for use by command definitions that execute a TCL script and wish to add to the stack trace on error as an aid to debugging.

§Example

let mut interp = Interp::new();
let input = "throw MYERR \"Error Message\"";
assert!(my_func(&mut interp, &input).is_err());

fn my_func(interp: &mut Interp, input: &str) -> MoltResult {
    // Evaluates the input; on error, adds some error info and rethrows.
    match interp.eval(input) {
       Ok(val) => Ok(val),
       Err(mut exception) => {
           if exception.is_error() {
               exception.add_error_info("in rustdoc example");
           }
           Err(exception)
       }
    }
}
§Panics

Panics if the exception is not an error exception.

Source

pub fn molt_err(msg: Value) -> Self

Creates an Error exception with the given error message. This is primarily intended for use by the molt_err! macro, but it can also be used directly.

§Example

let ex = Exception::molt_err("error message".into());
assert!(ex.is_error());
assert_eq!(ex.value(), "error message".into());
Source

pub fn molt_err2(error_code: Value, msg: Value) -> Self

Creates an Error exception with the given error code and message. An error code is a MoltList that indicates the nature of the error. Standard TCL uses the error code to flag specific arithmetic and I/O errors; most other errors have the code NONE. At present Molt doesn’t define any error codes other than NONE, so this method is primarily for use by the throw command; but use it if your code needs to provide an error code.

§Example

let ex = Exception::molt_err2("MYERR".into(), "error message".into());
assert!(ex.is_error());
assert_eq!(ex.error_code(), "MYERR".into());
assert_eq!(ex.value(), "error message".into());
Source

pub fn molt_return(value: Value) -> Self

Creates a Return exception, with the given return value. Return Value::empty() if there is no specific result.

This method is primarily for use by the return command, and should rarely if ever be needed in client code. If you fully understand the semantics of the return and catch commands, you’ll understand what this does and when you would want to use it. If you don’t, you almost certainly don’t need it.

Source

pub fn molt_return_ext( value: Value, level: usize, next_code: ResultCode, ) -> Self

Creates an extended Return exception with the given return value, -level, and -code. Return Value::empty() if there is no specific result.

It’s an error if level == 0 and next_code == Okay; that’s Ok(value) rather than an exception.

This method is primarily for use by the return command, and should rarely if ever be needed in client code. If you fully understand the semantics of the return and catch commands, you’ll understand what this does and when you would want to use it. If you don’t, you almost certainly don’t need it.

Source

pub fn molt_return_err( msg: Value, level: usize, error_code: Option<Value>, error_info: Option<Value>, ) -> Self

Creates an exception that will produce an Error exception with the given data, either immediately or some levels up the call chain. This is usually used to rethrow an existing error.

This method is primarily for use by the return command, and should rarely if ever be needed in client code. If you fully understand the semantics of the return and catch commands, you’ll understand what this does and when you would want to use it. If you don’t, you almost certainly don’t need it.

Source

pub fn molt_break() -> Self

Creates a Break exception.

This method is primarily for use by the break command, and should rarely if ever be needed in client code. If you fully understand the semantics of the return and catch commands, you’ll understand what this does and when you would want to use it. If you don’t, you almost certainly don’t need it.

Source

pub fn molt_continue() -> Self

Creates a Continue exception.

This method is primarily for use by the continue command, and should rarely if ever be needed in client code. If you fully understand the semantics of the return and catch commands, you’ll understand what this does and when you would want to use it. If you don’t, you almost certainly don’t need it.

Trait Implementations§

Source§

impl Clone for Exception

Source§

fn clone(&self) -> Exception

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Exception

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Exception

Source§

fn eq(&self, other: &Exception) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Exception

Source§

impl StructuralPartialEq for Exception

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.