Name
SPVM::Document::Language::ExceptionHandling - Exception Handling in SPVM Language
Description
This document describes exception handling in SPVM language.
Exception Handling
Throwing Exception
The die statement throws an exception.
die "This value is invalid.";
A basic type ID can be given to the die statement. This is set to eval_error_id
if an exception is thrown.
die basic_type_id Error::System, "This value is invalid.";
A class name can be given to the die statement. This is the same as the above code.
die Error::System "This value is invalid.";
If a basic type ID and a class name are not given, it is set to the basic type ID of the Error class.
Catching Exception
An eval block catches an exception.
eval {
die "This value is invalid.";
}
undef
is set to $@ at the beginning of each eval block.
0 is set to eval_error_id
at the beginning of each eval block.
If an exception is thrown, the message passed to the die statement is set to $@, and the basic type ID passed to the die statement is set to eval_error_id
.
if ($@) {
# Do something if an exception is thrwon
}
# If you need to check eval_error_id, write the following code.
if ($@) {
if (eval_error_id isa_error Error::System) {
}
}
$@
could be overwritten by the other operations, so it is good to save it into a local variable.
if (my $error = $@) {
}
Exception Variable
$@
is the exception variable. This is used to save a message for an exception.
$@
The type is the string type.
Using the assignment operator, the value of the exception variable can be set.
$@ = "Error Message";
$@ = undef;
The exception variable is a stack variable(not a global variable).
If a new stack is created for a thread, exception variables exist for each thread.
Copyright & License
Copyright (c) 2023 Yuki Kimoto
MIT License