@@ -82,20 +82,36 @@ The package allows objects to be instantiated with key-value arguments.
8282
8383=method throw
8484
85- The throw method throws an error with message.
85+ The throw method throws an error with message (and optionally, an ID) .
8686
8787=signature throw
8888
89- throw(Str $class , Any $context, Maybe[Number] $offset) : Any
89+ throw(Tuple[ Str, Str] | Str $message , Any $context, Maybe[Number] $offset) : Any
9090
9191=example-1 throw
9292
9393 use Data::Object::Exception;
9494
95+ my $exception = Data::Object::Exception->new;
96+
97+ $exception->throw('Oops!')
98+
99+ =example-2 throw
100+
101+ use Data::Object::Exception;
102+
95103 my $exception = Data::Object::Exception->new('Oops!');
96104
97105 $exception->throw
98106
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+
99115=cut
100116
101117=method explain
@@ -204,6 +220,34 @@ $subs->example(-1, 'throw', 'method', fun($tryable) {
204220 $result
205221});
206222
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+
207251$subs -> example(-1, ' explain' , ' method' , fun($tryable ) {
208252 ok my $result = $tryable -> result;
209253
0 commit comments