오류가 생성되면 예외가 발생합니다. 예외는 일반적으로 다음 멤버가 포함된 객체입니다.
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