Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
try catch scope
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Wes James  
View profile  
 More options Jun 29 2010, 5:21 pm
From: Wes James <compte...@gmail.com>
Date: Tue, 29 Jun 2010 15:21:12 -0600
Local: Tues, Jun 29 2010 5:21 pm
Subject: [erlang-questions] try catch scope
What is the scope of try/catch.

I changed the code:

has_query(A) ->
                case length(yaws_api:parse_query(A)) of
                        0  -> false;
                        _ -> true
                end.
to this

has_query(A) ->
        try
                case length(yaws_api:parse_query(A)) of
                        0  -> false;
                        _ -> true
                end
        catch
                _ -> false
        end.

because if a user puts in an invalid character in the URL it crashes
parse_query and I want the app to continue without erroring out in the
browser.

Is this because of the level of the error?

For instance if you have the url http://localhost/app?var1=foo1%var2=foo2

in parse_query it tries to do a list_to_integer of "va" with var2
based on how it parses the args list and you can't do that since "va"
doesn't produce a valid integer.  Would the try need to be at the
level of the list_to_integer call?

thx,

-wes

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sergey Samokhin  
View profile  
 More options Jun 29 2010, 5:46 pm
From: Sergey Samokhin <prikru...@gmail.com>
Date: Wed, 30 Jun 2010 01:46:07 +0400
Local: Tues, Jun 29 2010 5:46 pm
Subject: Re: [erlang-questions] try catch scope

> 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-unsubscr...@erlang.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jesper Louis Andersen  
View profile  
 More options Jun 29 2010, 5:53 pm
From: Jesper Louis Andersen <jesper.louis.ander...@gmail.com>
Date: Tue, 29 Jun 2010 23:53:04 +0200
Local: Tues, Jun 29 2010 5:53 pm
Subject: Re: [erlang-questions] try catch scope

On Tue, Jun 29, 2010 at 11:21 PM, Wes James <compte...@gmail.com> wrote:
> What is the scope of try/catch.
> has_query(A) ->
>        try
>                case length(yaws_api:parse_query(A)) of

[...]

> because if a user puts in an invalid character in the URL it crashes
> parse_query and I want the app to continue without erroring out in the
> browser.

> Is this because of the level of the error?

Yes (is my guess), see
http://www.erlang.org/doc/reference_manual/expressions.html#id2276021
on the try..catch construction. Since you omit the class, it is
defaulted to 'throw'. I would personally regard that as an error in
the interface of parse_query. My solution would be something akin to

safe_parse_query(A) ->
  try
    Q = yaws_api:parse_query(A),
    {ok, Q}
  catch
    error:_ -> no_parse % You should really narrow down the class and
the error Reason to the right type here. I may be wrong.
  end.

which transforms the function into a variant which is safer:

has_query(A) ->
  case yaws_api:parse_query(A) of
    no_parse -> false;
    {ok, Q} when length(Q) == 0 -> false;
    {ok, Q} -> true
  end.

alternatively, no_parse can be an empty list, if you so want.

(Caveat: I only wrote the code and did not test it).

--
J.

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wes James  
View profile  
 More options Jun 29 2010, 6:17 pm
From: Wes James <compte...@gmail.com>
Date: Tue, 29 Jun 2010 16:17:02 -0600
Local: Tues, Jun 29 2010 6:17 pm
Subject: Re: [erlang-questions] try catch scope
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

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Virding  
View profile  
 More options Jun 29 2010, 7:35 pm
From: Robert Virding <rvird...@gmail.com>
Date: Wed, 30 Jun 2010 01:35:45 +0200
Local: Tues, Jun 29 2010 7:35 pm
Subject: Re: [erlang-questions] try catch scope
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 <compte...@gmail.com> wrote:

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Magnus Henoch  
View profile  
 More options Jun 30 2010, 7:53 am
From: Magnus Henoch <mag...@erlang-solutions.com>
Date: Wed, 30 Jun 2010 12:53:30 +0100
Local: Wed, Jun 30 2010 7:53 am
Subject: [erlang-questions] Re: try catch scope

Wes James <compte...@gmail.com> writes:
> has_query(A) ->
>            case length(yaws_api:parse_query(A)) of
>                    0  -> false;
>                    _ -> true
>            end.

A separate note about the code: you should avoid "case length(Foo)" when
possible, since length/1 needs to traverse the entire list.  Just
comparing the result to the empty list is probably faster:

case yaws_api:parse_query(A) of
    [] -> false;
    _ -> true
end.

And this can be simplified further:

yaws_api:parse_query(A) /= [].

(There is a difference between my two examples and yours: your code will
crash with badarg if the result is not a proper list, while mine will
just return false.)

--
Magnus Henoch, mag...@erlang-solutions.com
Erlang Solutions
http://www.erlang-solutions.com/

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wes James  
View profile  
 More options Jun 30 2010, 9:33 am
From: Wes James <compte...@gmail.com>
Date: Wed, 30 Jun 2010 07:33:39 -0600
Local: Wed, Jun 30 2010 9:33 am
Subject: Re: [erlang-questions] Re: try catch scope
Mangus,

Thanks! This is much better!

has_query(A) ->
        try
                yaws_api:parse_query(A) /= []
        catch
                _:_ -> false
        end.

-wes

On Wed, Jun 30, 2010 at 5:53 AM, Magnus Henoch

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Raimo Niskanen  
View profile  
 More options Jun 30 2010, 10:17 am
From: Raimo Niskanen <raimo+erlang-questi...@erix.ericsson.se>
Date: Wed, 30 Jun 2010 16:17:31 +0200
Local: Wed, Jun 30 2010 10:17 am
Subject: Re: [erlang-questions] Re: try catch scope

On Wed, Jun 30, 2010 at 07:33:39AM -0600, Wes James wrote:
> Mangus,

> Thanks! This is much better!

> has_query(A) ->
>    try
>            yaws_api:parse_query(A) /= []
>    catch
>            _:_ -> false
>    end.

> -wes

This might be even clearer (separating the right and wrong cases):

has_query(A) ->
    try yaws_api:parse_query(A) of
        [] -> false;
        _ -> true % Were you supposed to return true for this case?
    catch
        error:_ -> false % Only cathes runtime errors, not exit/1.
    end.

--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wes James  
View profile   Translate to Translated (View Original)
 More options Jun 30 2010, 6:43 pm
From: Wes James <compte...@gmail.com>
Date: Wed, 30 Jun 2010 16:43:31 -0600
Local: Wed, Jun 30 2010 6:43 pm
Subject: Re: [erlang-questions] Re: try catch scope
On Wed, Jun 30, 2010 at 8:17 AM, Raimo Niskanen

Thanks.  This works too.  Bottom line, is I want to show the default
search page even if the parse_query call throws an error.  Maybe
someone is trying to insert bad chars or such in to the URL to try to
reveal something or even hack the system, etc.

thx,

-wes

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questions-unsubscr...@erlang.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »