Error handlers
Green Tea provide 2 ways to handle error:
error_handler_function
try…catch…finally structures
defcat…deffin (default catch)
error_handler_function
You could declare this function to handle how to show when unhamdled error happened
@error_handler_function($errno,
$errstr, $errfile, $errline)
//do
somethings
errno- The first parameter,
errno, will be passed the level of the error raised, as an integer. errstr- The second parameter,
errstr, will be passed the error message, as a string. errfile- If the callback accepts a third parameter,
errfile, it will be passed the filename that the error was raised in, as a string. errline- If the callback accepts a fourth parameter,
errline, it will be passed the line number where the error was raised, as an integer. errcontext- If the callback accepts a fifth parameter,
errcontext, it will be passed an array that points to the active symbol table at the point the error occurred. In other words,errcontextwill contain an array of every variable that existed in the scope the error was triggered in. User error handlers must not modify the error context.
try…catch…finally structures
Used to catch possible exception in try block. If a Exception match multiple catches, only the 1st catch block matched will run. Finally block run whenever any Exception is catch or not.
Ex: If ^DevidedByZeroException happened ($num2=0), catch block 1 will be triggered). If other exception happened, catch block 2 will run. Finally block run whenever any Exception is catch or not.
@devide $num1 $num2
try
$result = $num1 / $num2
catch ^DevidedByZeroException
// catch block 1
@echo “devided by zero”
$result = false
catch ^Exception
// catch block 2
@cho “other exception”
$result = false
finally
return $result
defcat…deffin structures
You could use defcat as default catch for functions, which will catch Exceptions when running current function
defcat and deffin should be at the end of function
@devide $num1 $num2
$result = $num1 / $num2
defcat
^DevidedByZeroException
// catch block 1
@echo “devided by zero”
$result = false
^Exception
// catch block 2
@cho “other exception”
$result = false
deffin
return $result
You could use defcat and deffin outside of functions, which will handle exception for the whole program. They should be at the end of source files.
