Skip to main content

Error

Struct Error 

Source
pub struct Error {
    pub kind: Kind,
    pub message: String,
    /* private fields */
}
Expand description

A raised exception, on its way out of the runtime.

Not the same thing as an Exception, which is the object a program holds. This is the Rust error the ? in every operation propagates, and it is a kind and a message because that is all a division by zero has: no Python object is made for one unless something asks for it.

Fields§

§kind: Kind

Which one it is.

§message: String

What it says, which is empty for the ones that say nothing.

Implementations§

Source§

impl Error

Source

pub fn new(kind: Kind, message: impl Into<String>) -> Self

An exception of this kind with this message.

Source

pub fn type_error(message: impl Into<String>) -> Self

The wrong type for the operation.

Source

pub fn value_error(message: impl Into<String>) -> Self

The right type and the wrong value.

Source

pub fn zero_division(message: impl Into<String>) -> Self

A divisor that was zero.

Source

pub fn overflow(message: impl Into<String>) -> Self

A result that will not fit.

Source

pub fn raised(kind: Kind, args: Vec<Object>) -> Self

An exception built from the arguments a program would have written.

The message comes out of the arguments rather than being given separately, so an exception the runtime raises this way is the one a handler catches: {}['k'] raises a KeyError whose args really is ('k',) and not a KeyError holding the string 'k' with its quotes already in it.

Source

pub fn with_value(self, value: Object) -> Self

The same exception, carrying the object a program raised.

Carried rather than rebuilt at the catch, because raise e and the except ... as e that catches it have to be the same object and there is no way back to it from a kind and a message.

Source

pub fn value(&self) -> Option<&Object>

The object that was raised, for a caller that has to hand back the very one. None when the runtime raised this itself.

Source

pub fn instance(&self) -> Object

The exception as an object, which is what an except clause tests and what as binds.

The one a program raised when there is one, so that raise e and the except ... as e catching it are the same object. One built here otherwise, out of the message, because a division by zero never made an object and except ZeroDivisionError as e still has to have something to bind. Its arguments come out the way CPython’s do, which is one argument holding the sentence, or none when there is no sentence.

A Kind::KeyError is the one that cannot be rebuilt from its message, since its message is already the repr of its key. That is why every KeyError the runtime raises is built with Error::raised and carries its key.

Trait Implementations§

Source§

impl Clone for Error

Source§

fn clone(&self) -> Error

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Error

Source§

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

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

impl Display for Error

Source§

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

The tail of a traceback: every exception that led to this one, oldest first, and then this one.

There are no File "x", line n lines in between because there is no line table yet, so what comes out is the part of a traceback that says what happened without the part that says where.

Source§

impl Error for Error

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Error

§

impl !Send for Error

§

impl !Sync for Error

§

impl !UnwindSafe for Error

§

impl Freeze for Error

§

impl Unpin for Error

§

impl UnsafeUnpin for Error

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<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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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.