[erlang-questions] try catch scope

Robert Virding rvirding@REDACTED
Wed Jun 30 01:35:45 CEST 2010


No _:_ is *NOT* the same as {_,_}. It is a special pattern which can
only be used in the catch part of a try. There you can give
[Class1:]ExceptionPattern1 as a pattern to match which class of
exception to match and also which exception. Writing _:_ here means
that you match all classes and all exceptions, i.e. you catch
everything.

Calling throw an exception is not really correct in my opinion as it
doesn't really signify an error but is a non-local return. At least
that is how it was intended to be used. Try might actually be too
powerful.

Robert

On 30 June 2010 00:17, Wes James <comptekki@REDACTED> wrote:
> Thank you Sergey and Jesper for your responses. I went back and looked at:
>
> http://www.erlang.org/doc/reference_manual/expressions.html#id2275780
> and
> http://www.erlang.org/doc/reference_manual/errors.html
>
> to see how _:_ works.  There is no mention of this on these pages.  Is
> _:_ the same as {_,_}?  Is there a doc on what _:_ is?
>
> thx, again,
>
> -wes
>
> On Tue, Jun 29, 2010 at 3:46 PM, Sergey Samokhin <prikrutil@REDACTED> wrote:
>>> has_query(A) ->
>>>        try
>>>                case length(yaws_api:parse_query(A)) of
>>>                        0  -> false;
>>>                        _ -> true
>>>                end
>>>        catch
>>>                _ -> false
>>>        end.
>>
>> For this function to catch exceptions of all possible types (error,
>> exit and throw) you need to use "_:_" pattern instead of just "_"
>> (which will catch only 'throw' exceptions):
>>
>> has_query(A) ->
>>       try
>>               case length(yaws_api:parse_query(A)) of
>>                       0  -> false;
>>                       _ -> true
>>               end
>>       catch
>>               _:_ -> false % <----- Here "_" has been replaced by "_:_"
>>       end.
>>
>> --
>> Sergey Samokhin
>>
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list