Trait rutie::Exception[][src]

pub trait Exception: Object {
    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 { ... } }

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

fn new(class: &str, msg: Option<&str>) -> Self[src]

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"
);

fn exception(&self, string: Option<&str>) -> Self[src]

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"))
);

fn backtrace(&self) -> Option<Array>[src]

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());

fn backtrace_locations(&self) -> Option<Array>[src]

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());

fn cause(&self) -> Option<Self>[src]

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());

fn inspect(&self) -> String[src]

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>"
);

fn message(&self) -> String[src]

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"
);

fn set_backtrace(&self, backtrace: AnyObject) -> Option<Array>[src]

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"
);

fn to_s(&self) -> String[src]

Returns exception's message (or the name of the exception if no message is set).

Examples

use rutie::{AnyException, Exception, Object, VM};

assert_eq!(
  AnyException::new("StandardError", Some("oops")).to_s(),
  "oops"
);
Loading content...

Implementors

impl Exception for AnyException[src]

Loading content...