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: KindWhich one it is.
message: StringWhat it says, which is empty for the ones that say nothing.
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(kind: Kind, message: impl Into<String>) -> Self
pub fn new(kind: Kind, message: impl Into<String>) -> Self
An exception of this kind with this message.
Sourcepub fn type_error(message: impl Into<String>) -> Self
pub fn type_error(message: impl Into<String>) -> Self
The wrong type for the operation.
Sourcepub fn value_error(message: impl Into<String>) -> Self
pub fn value_error(message: impl Into<String>) -> Self
The right type and the wrong value.
Sourcepub fn zero_division(message: impl Into<String>) -> Self
pub fn zero_division(message: impl Into<String>) -> Self
A divisor that was zero.
Sourcepub fn raised(kind: Kind, args: Vec<Object>) -> Self
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.
Sourcepub fn with_value(self, value: Object) -> Self
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.
Sourcepub fn value(&self) -> Option<&Object>
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.
Sourcepub fn instance(&self) -> Object
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 Display for Error
impl Display for Error
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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
impl Error for Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()