(patch for Bash) try-block and exception

W

William Park

(crossposted to comp.lang.python, because this may be of interest to
them.)

Python has try-block, within which you can raise exception. Once it's
raised, execution breaks out of the try-block and is caught at the end
of try-block.

Now, Bash has similiar feature. I've added try-block and 'raise'
builtin into Bash-3.0. Typical usage would go something like
try
echo a
raise
echo b
done
or
try
echo a
raise 2
echo b
done in
0) echo okey ;;
1) echo raised 1 ;;
2) echo raised 2 ;;
*) echo really bad ;;
esac

The exception is positive integer and is raised by 'raise' builtin, just
like 'break' for the for/while/until loops. And, it can be caught by
using optional case-like statement.

Ref:
http://freshmeat.net/projects/bashdiff/
help try
help raise
 
W

William Park

In said:
(crossposted to comp.lang.python, because this may be of interest to
them.)

Python has try-block, within which you can raise exception. Once it's
raised, execution breaks out of the try-block and is caught at the end
of try-block.

Now, Bash has similiar feature. I've added try-block and 'raise'
builtin into Bash-3.0. Typical usage would go something like
try
echo a
raise
echo b
done
or
try
echo a
raise 2
echo b
done in
0) echo okey ;;
1) echo raised 1 ;;
2) echo raised 2 ;;
*) echo really bad ;;
esac

Typo... I pasted an old example. No globbing or any shell expansion is
done.
try
echo a
raise 2
echo b
done in
0) echo okey ;;
1) echo raised 1 ;;
2) echo raised 2 ;;
esac
 
K

Kenny McCormack

William Park said:
Next feature I'll tackle is list comprehension. :)

Are you saying that you don't understand how lists work?
If so, post an item here - I'm sure people will be more than willing to help.
 
G

Greg Ewing

William said:
try
echo a
raise 2
echo b
done in
0) echo okey ;;
1) echo raised 1 ;;
2) echo raised 2 ;;
*) echo really bad ;;
esac

try...done...esac? What a delightfully eclectic combination
of syntax. :)

Why doesn't it end with 'yrt'?
 
W

William Park

In said:
try...done...esac? What a delightfully eclectic combination
of syntax. :)

Why doesn't it end with 'yrt'?

'try-done' was chosen because it resembles while-loop where you would
break out of; and, '-in-esac' was chosen because it resembles case
statement. Also, I didn't want to introduce too many new keywords.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top