Перехват конкретного исключения

Обработка исключений

#each error has its own exit code
throwWhenNullOrEmpty() {
    if (($# == 0)); then
        return 1
    fi
    if [[ -z $1 ]]; then
        return 2
    fi
}

#case on $? picks the handler for the code
throwWhenNullOrEmpty ""
case $? in
    0) echo "text is fine" ;;
    1) echo "text is not specified" ;;
    2) echo "text is empty" ;;
    *) echo "unknown error" ;;
esac
#prints "text is empty"