throw. Reporting an error

^throw[type]   [3.3.0]
^throw[type;source]
^throw[type;source;comment]
^throw[hash]

Operator throw reports error of type, which was caused by source, and provides comment.

This error can be intercepted and handled by using operator
try.

Do not intercept errors only to provide a good-looking output. Let method
unhandled_exception, do it all instead, if no handler can be found. Besides, the method will add entries to server's error log, which you can regularly look through to find problems that might crop up.

Example
@method[command]
^
switch[$command]{
   ^
case[add]{
      adding
   }
   ^case[delete]{
      deleting
   }
   ^case[
DEFAULT]{
      ^throw[bad.command;$command;
Wrong command $command, good are add&delete]
      ^rem{
         the next format also acceptable:
         ^throw[
            $.type[
bad.command]
            $.source[
$command]
            $.comment[
Wrong command $command, good are add&delete]
         ]
      }

   }
}

@
main[]
$action[format c:]
^
try{
   ^method[$action]
}{
   ^if($
exception.type eq bad.command){
      $exception.handled(
true)
      
Wrong command '$exception.source', in file $exception.filein line $exception.lineno.
   }
}


The result of this code's work will be:
Wrong command 'format c:', in file c:/parser3tests/www/htdocs/throw.html, in line 15.

We would like to remind you that visitors should not see errors' technical details, especially if such details contain paths to files-it is both ugly and unsafe.
Outputting
$exception.file is nothing but an example that you can use while debugging the site at server, but by no means in production mode.


Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 18.07.2008