当生成错误时,会引发异常。异常通常是包含以下成员的对象:
code-与错误关联的数字代码
message-与错误关联的用户可读消息
异常可以被捕获并处理,否则可能会在脚本环境中被引发。如果脚本环境存在未捕获的异常,则 CLI 将显示详细信息。例如:
dory:> script run('not a cmd')
error: uncaught error exception (code EAKSH_BADCMD) in script: invalid command
"not a cmd" (encountered while attempting to run command "not a cmd")
可以通过捕获并转储异常来了解有关该异常的更多详细信息:
dory:> script try { run('not a cmd') } catch (err) { dump(err); }
{
toString: <function>,
code: 10004,
message: 'invalid command "not a cmd" (encountered while attempting to
run command "not a cmd")'
}
这还使您拥有丰富的错误处理方法,例如:
#!/usr/bin/ksh -p
ssh -T root@dory <<EOF
script
try {
run('shares select default select $1');
} catch (err) {
if (err.code == EAKSH_ENTITY_BADSELECT) {
printf('error: "$1" is not a share in the ' +
'default project\n');
exit(1);
}
throw (err);
}
printf('"default/$1": compression is %s\n', get('compression'));
exit(0);
EOF
如果该脚本的名称为 "share.ksh" 并使用无效的共享资源名称运行,将生成丰富的错误消息:
% ksh ./share.ksh bogus error: "bogus" is not a share in the default project