« blib/lib/SPVM/Document/Language/Class.pm
↑ Summary
blib/lib/SPVM/Document/Language/GarbageCollection.pm »
| 1 | =head1 Name | ||
| 2 | |||
| 3 | SPVM::Document::Language::ExceptionHandling - Exception Handling in the SPVM Language | ||
| 4 | |||
| 5 | =head1 Description | ||
| 6 | |||
| 7 | This document describes exception handling in the SPVM language. | ||
| 8 | |||
| 9 | =head1 Exception Handling | ||
| 10 | |||
| 11 | =head2 Throwing Exception | ||
| 12 | |||
| 13 | The L<die statement|SPVM::Document::Language::Statements/"die Statement"> throws an exception. | ||
| 14 | |||
| 15 | die "This value is invalid."; | ||
| 16 | |||
| 17 | A basic type ID can be given to die statement. This is set to C<eval_error_id> if an exception is thrown. | ||
| 18 | |||
| 19 | die basic_type_id Error::System, "This value is invalid."; | ||
| 20 | |||
| 21 | A class name can be given to die statement. This is the same as the above code. | ||
| 22 | |||
| 23 | die Error::System "This value is invalid."; | ||
| 24 | |||
| 25 | If a basic type ID and a class name are not given, it is set to the basic type ID of L<Error|SPVM::Error> class. | ||
| 26 | |||
| 27 | =head2 Catching Exception | ||
| 28 | |||
| 29 | An eval block catches an exception. | ||
| 30 | |||
| 31 | eval { | ||
| 32 | die "This value is invalid."; | ||
| 33 | } | ||
| 34 | |||
| 35 | C<undef> is set to L<$@|/"Exception Variable"> at the beginning of each eval block. | ||
| 36 | |||
| 37 | 0 is set to C<eval_error_id> at the beginning of each eval block. | ||
| 38 | |||
| 39 | If an exception is thrown, the message passed to die statement is set to L<$@|/"Exception Variable">, and the basic type ID passed to die statement is set to C<eval_error_id>. | ||
| 40 | |||
| 41 | if ($@) { | ||
| 42 | # Do something if an exception is thrwon | ||
| 43 | } | ||
| 44 | |||
| 45 | # If you need to check eval_error_id, write the following code. | ||
| 46 | if ($@) { | ||
| 47 | if (eval_error_id isa_error Error::System) { | ||
| 48 | |||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | C<$@> could be overwritten by the other operations, so it is good to save it into a local variable. | ||
| 53 | |||
| 54 | if (my $error = $@) { | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | =head2 Exception Variable | ||
| 59 | |||
| 60 | C<$@> is the exception variable. This is used to save a message for an exception. | ||
| 61 | |||
| 62 | $@ | ||
| 63 | |||
| 64 | The type is string type. | ||
| 65 | |||
| 66 | Using assignment operator, the value of the exception variable can be set. | ||
| 67 | |||
| 68 | $@ = "Error Message"; | ||
| 69 | |||
| 70 | $@ = undef; | ||
| 71 | |||
| 72 | The exception variable is a stack variable(not a global variable). | ||
| 73 | |||
| 74 | If a new stack is created for a thread, exception variables exist for each thread. | ||
| 75 | |||
| 76 | =head1 See Also | ||
| 77 | |||
| 78 | =over 2 | ||
| 79 | |||
| 80 | =item * L<SPVM::Document::Language::Statements> | ||
| 81 | |||
| 82 | =item * L<SPVM::Document::Language::Operators> | ||
| 83 | |||
| 84 | =item * L<SPVM::Document::Language> | ||
| 85 | |||
| 86 | =item * L<SPVM::Document> | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =head1 Copyright & License | ||
| 91 | |||
| 92 | Copyright (c) 2023 Yuki Kimoto | ||
| 93 | |||
| 94 | MIT License |