pub trait Exception: Object {
// Provided methods
fn new(class: &str, msg: Option<&str>) -> Self { ... }
fn exception(&self, string: Option<&str>) -> Self { ... }
fn backtrace(&self) -> Option<Array> { ... }
fn backtrace_locations(&self) -> Option<Array> { ... }
fn cause(&self) -> Option<Self> { ... }
fn inspect(&self) -> String { ... }
fn message(&self) -> String { ... }
fn set_backtrace(&self, backtrace: AnyObject) -> Option<Array> { ... }
fn to_s(&self) -> String { ... }
}
Expand description
Descendants of class Exception are used to communicate between Kernel#raise
and rescue statements in begin ... end
blocks. Exception objects carry
information about the exception – its type (the exception’s class name), an
optional descriptive string, and optional traceback information. Exception
subclasses may add additional information like NameError#name.
Programs may make subclasses of Exception, typically of StandardError or
RuntimeError, to provide custom classes and add additional information.
See the subclass list below for defaults for raise
and rescue
.
Provided Methods§
sourcefn new(class: &str, msg: Option<&str>) -> Self
fn new(class: &str, msg: Option<&str>) -> Self
Construct a new Exception object, optionally passing in a message.
Examples
use rutie::{AnyException, Exception, Object, VM};
assert_eq!(
AnyException::new("StandardError", None).to_s(),
"StandardError"
);
A nested exception
use rutie::{AnyException, Exception, Object, VM, Class};
let mut klass = Class::new("MyGem", None);
let se = Class::from_existing("StandardError");
let _ = klass.define_nested_class("MyError", Some(&se));
assert_eq!(
AnyException::new("MyGem::MyError", None).to_s(),
"MyGem::MyError"
);
sourcefn exception(&self, string: Option<&str>) -> Self
fn exception(&self, string: Option<&str>) -> Self
With no argument, or if the argument is the same as the receiver,
return the receiver. Otherwise, create a new exception object of
the same class as the receiver, but with a message equal
to string.to_str
.
Examples
use rutie::{AnyException, Exception, Object, VM};
assert_eq!(
AnyException::new("StandardError", Some("something went wrong")).exception(None),
AnyException::new("StandardError", Some("something went wrong"))
);
sourcefn backtrace(&self) -> Option<Array>
fn backtrace(&self) -> Option<Array>
Returns any backtrace associated with the exception. The backtrace is an array of strings, each containing either “filename:lineNo: in `method’‘’ or “filename:lineNo.‘’
Examples
use rutie::{AnyException, Exception, Object, VM, RString};
let x = AnyException::new("StandardError", Some("something went wrong"));
assert!(x.backtrace().is_none());
sourcefn backtrace_locations(&self) -> Option<Array>
fn backtrace_locations(&self) -> Option<Array>
Returns any backtrace associated with the exception. This method is similar to #backtrace, but the backtrace is an array of Thread::Backtrace::Location.
Now, this method is not affected by #set_backtrace.
Examples
use rutie::{AnyException, Exception, Object, VM, RString};
let x = AnyException::new("StandardError", Some("something went wrong"));
assert!(x.backtrace_locations().is_none());
sourcefn cause(&self) -> Option<Self>
fn cause(&self) -> Option<Self>
Returns the previous exception at the time this exception was raised. This is useful for wrapping exceptions and retaining the original exception information.
Examples
use rutie::{AnyException, Exception, Object, VM, RString};
let x = AnyException::new("StandardError", Some("something went wrong"));
assert!(x.cause().is_none());
sourcefn inspect(&self) -> String
fn inspect(&self) -> String
Return this exception’s class name and message
Examples
use rutie::{AnyException, Exception, Object, VM};
assert_eq!(
AnyException::new("StandardError", Some("oops")).inspect(),
"#<StandardError: oops>"
);
sourcefn message(&self) -> String
fn message(&self) -> String
Returns the result of invoking exception.to_s
. Normally this
returns the exception’s message or name.
Examples
use rutie::{AnyException, Exception, Object, VM};
assert_eq!(
AnyException::new("StandardError", Some("oops")).message(),
"oops"
);
sourcefn set_backtrace(&self, backtrace: AnyObject) -> Option<Array>
fn set_backtrace(&self, backtrace: AnyObject) -> Option<Array>
Sets the backtrace information associated with exc. The backtrace must be an array of String objects or a single String in the format described in #backtrace.
Examples
use rutie::{AnyException, Exception, Object, VM, RString, Array};
let x = AnyException::new("StandardError", Some("something went wrong"));
let mut arr = Array::new();
arr.push(RString::new_utf8("prog.rb:10"));
x.set_backtrace(arr.to_any_object());
assert_eq!(
x.backtrace().
unwrap().
pop().
try_convert_to::<RString>().
unwrap().
to_string(),
"prog.rb:10"
);