class X::Attribute::Required
Compilation error due to not declaring an attribute with the is required trait
does X::MOP
Compile time error thrown when a required attribute is not assigned when creating an object.
For example
my;my = Uses-required.new()
Dies with
OUTPUT: «(exit code 1) The attribute '$!req' is required, but you did not provide a value for it.»
Methods
method name
method name(--> Str)
Returns the name of the attribute.
method why
method why(--> Str)
Returns the reason why that attribute is required, and it will be included in the message if provided. That reason is taken directly from the is required
trait.
my;my = Uses-required.new(); │# OUTPUT:# «(exit code 1) The attribute '$!req' is required because because yes,# but you did not provide a value for it.»
Type Graph
Routines supplied by class Exception
X::Attribute::Required inherits from class Exception, which provides the following routines:
(Exception) method message
Defined as:
method message(Exception: --> Str)
This is a stub that must be overwritten by subclasses, and should return the exception message.
Special care should be taken that this method does not produce an exception itself.
try die "Something bad happened";if ($!)
(Exception) method backtrace
Defined as:
method backtrace(Exception:)
Returns the backtrace associated with the exception in a Backtrace
object or an empty string if there is none. Only makes sense on exceptions that have been thrown at least once.
try die "Something bad happened";with $!
(Exception) method throw
Defined as:
method throw(Exception:)
Throws the exception.
my = X::AdHoc.new; # Totally finetry .throw; # Throwsif ($!) ; # Suppress the exception
(Exception) method resume
Defined as:
method resume(Exception:)
Resumes control flow where .throw
left it when handled in a CATCH
block.
# For example, resume control flow for any exceptionCATCH
(Exception) method rethrow
Defined as:
method rethrow(Exception:)
Rethrows an exception that has already been thrown at least once. This is different from throw
in that it preserves the original backtrace.
my = X::AdHoc.new(payload => "Bad situation");sub f() ;sub g() ;g;CATCH ;
(Exception) method fail
Defined as:
multi sub fail(*)multi sub fail(Exception )method fail(Exception:)
Exits the calling Routine
and returns a Failure object wrapping the exception $e
- or, for the *@text
form, an X::AdHoc exception constructed from the concatenation of @text
. If the caller activated fatal exceptions via the pragma use fatal;
, the exception is thrown instead of being returned as a Failure
.
# A custom exception definedis Exceptionsub copy-directory-tree ()# A Failure with X::AdHoc exception object is returned and# assigned, so no throwing Would be thrown without an assignmentmy = copy-directory-tree("cat.jpg");say .exception; # OUTPUT: «cat.jpg is not a directory»# A Failure with a custom Exception object is returned= copy-directory-tree('foo');say .exception; # OUTPUT: «This directory is forbidden: 'foo'»
(Exception) method gist
Defined as:
multi method gist(Exception:)
Returns whatever the exception printer should produce for this exception. The default implementation returns message and backtrace separated by a newline.
my = X::AdHoc.new(payload => "This exception is pretty bad");try .throw;if ($!) ;# OUTPUT: «This exception is pretty bad# in block <unit> at <unknown file> line 1»
(Exception) sub die
Defined as:
multi sub die()multi sub die(*)multi sub die(Exception )method die(Exception:)
Throws a fatal Exception. The default exception handler prints each element of the list to $*ERR
(STDERR).
die "Important reason";
If the subroutine form is called without arguments, the value of $!
variable is checked. If it is set to a .DEFINITE
value, its value will be used as the Exception to throw if it's of type Exception, otherwise, it will be used as payload of X::AdHoc exception. If $!
is not .DEFINITE
, X::AdHoc with string "Died"
as payload will be thrown.
die
will print by default the line number where it happens
die "Dead";# OUTPUT: «(exit code 1) Dead# in block <unit> at /tmp/dead.p6 line 1»
However, that default behavior is governed at the Exception
level and thus can be changed to anything we want by capturing the exception using CATCH
. This can be used, for instance, to suppress line numbers.
CATCH;die "Dead" # OUTPUT: «Dead»
(Exception) sub warn
Defined as:
multi sub warn(*)
Throws a resumable warning exception, which is considered a control exception, and hence is invisible to most normal exception handlers. The outermost control handler will print the warning to $*ERR
. After printing the warning, the exception is resumed where it was thrown. To override this behavior, catch the exception in a CONTROL
block. A quietly {...}
block is the opposite of a try {...}
block in that it will suppress any warnings but pass fatal exceptions through.
To simply print to $*ERR
, please use note
instead. warn
should be reserved for use in threatening situations when you don't quite want to throw an exception.
warn "Warning message";