[erlang-bugs] try in shell

Tony Rogvall tony@REDACTED
Wed Sep 17 00:18:28 CEST 2014


Hi!

Not sure how this is supposed to be, given that there is a shell function called catch_exception/1.

Running in 17.1 (approx)

> try throw(x) catch Class:Reason -> {Class,Reason} end.
{throw,x}

> try exit(2) catch Class:Reason -> {Class,Reason} end.          
* exception exit: 2

> try foo:bar() catch Class:Reason -> {Class,Reason} end.        
* exception error: undefined function erl_eval:expr/3

> try 1/0 catch Class:Reason -> {Class,Reason} end.       
* exception error: an error occurred when evaluating an arithmetic expression

> try (a=b) catch Class:Reason -> {Class,Reason} end.
* exception error: no match of right hand side value b

Putting the above try expression in a module:

-module(tryme).
-compile(export_all).

test1() ->
    try throw(x) catch Class:Reason -> {Class,Reason} end.

test2() ->
    try exit(2) catch Class:Reason -> {Class,Reason} end.

test3() ->
    try (a=b) catch Class:Reason -> {Class,Reason} end.

test4() ->
    try 1/0 catch Class:Reason -> {Class,Reason} end.

test5() ->
    try foo:bar() catch Class:Reason -> {Class,Reason} end.


First compile:
tryme.erl:11: Warning: no clause will ever match
tryme.erl:14: Warning: this expression will fail with a 'badarith' exception

On line 11 there are no try clauses, but catch clauses that will always match, so the warning is a bit strange.
And the expression on line 14 is a try, so it will never fail with badarith, possibly return an error tuple lets see:

> tryme:test1().
{throw,x}
> tryme:test2().
{exit,2}
> tryme:test3().
{error,{badmatch,b}}
> tryme:test4().
{error,badarith}
> tryme:test5().
{error,undef}

This looks a bit more like I would expect.

Comments?

/Tony








More information about the erlang-bugs mailing list