@@ -27,6 +27,14 @@ method: trace
2727
2828=cut
2929
30+ =attributes
31+
32+ id: ro, opt, Str
33+ context: ro, opt, Any
34+ message: ro, opt, Str
35+
36+ =cut
37+
3038=synopsis
3139
3240 use Data::Object::Exception;
@@ -74,20 +82,36 @@ The package allows objects to be instantiated with key-value arguments.
7482
7583=method throw
7684
77- The throw method throws an error with message.
85+ The throw method throws an error with message (and optionally, an ID) .
7886
7987=signature throw
8088
81- throw(Str $class , Any $context, Maybe[Number] $offset) : Any
89+ throw(Tuple[ Str, Str] | Str $message , Any $context, Maybe[Number] $offset) : Any
8290
8391=example-1 throw
8492
8593 use Data::Object::Exception;
8694
95+ my $exception = Data::Object::Exception->new;
96+
97+ $exception->throw('Oops!')
98+
99+ =example-2 throw
100+
101+ use Data::Object::Exception;
102+
87103 my $exception = Data::Object::Exception->new('Oops!');
88104
89105 $exception->throw
90106
107+ =example-3 throw
108+
109+ use Data::Object::Exception;
110+
111+ my $exception = Data::Object::Exception->new;
112+
113+ $exception->throw(['E001', 'Oops!'])
114+
91115=cut
92116
93117=method explain
@@ -196,6 +220,34 @@ $subs->example(-1, 'throw', 'method', fun($tryable) {
196220 $result
197221});
198222
223+ $subs -> example(-2, ' throw' , ' method' , fun($tryable ) {
224+ $tryable -> default(fun($caught ) {
225+ $caught
226+ });
227+ ok my $result = $tryable -> result;
228+ ok $result -> isa(' Data::Object::Exception' );
229+ ok !length $result -> id;
230+ ok !length $result -> context;
231+ ok length $result -> frames;
232+ is $result -> message, ' Oops!' ;
233+
234+ $result
235+ });
236+
237+ $subs -> example(-3, ' throw' , ' method' , fun($tryable ) {
238+ $tryable -> default(fun($caught ) {
239+ $caught
240+ });
241+ ok my $result = $tryable -> result;
242+ ok $result -> isa(' Data::Object::Exception' );
243+ is $result -> id, ' E001' ;
244+ ok !length $result -> context;
245+ ok length $result -> frames;
246+ is $result -> message, ' Oops!' ;
247+
248+ $result
249+ });
250+
199251$subs -> example(-1, ' explain' , ' method' , fun($tryable ) {
200252 ok my $result = $tryable -> result;
201253
0 commit comments