I regret my one word answer because while it answers the question, there's a question of good practice to consider.

Putting "return ball;" as the last line doesn't really help the casual code reader, because I need to really look hard at the code to realise that if ball is not explicitly set elsewhere the return value will be null.

However,

catch (e:TypeError ){ return null; }

explicitly shows that if the exception occurs the function will return null.

There's even a good argument for having "return null" at the end of the code too, since only one exception will explicitly return null...

It's always better to have explicit code rather than try and save a statement or two. We've all been down the "smart" code route though. Guilty as charged.

Paul





----- Original Message ----- From: "Paul Andrews" <[email protected]>
To: "Flash Coders List" <[email protected]>
Sent: Saturday, May 09, 2009 4:17 PM
Subject: Re: [Flashcoders] Try... catch......


Yes.

----- Original Message ----- From: "ACE Flash" <[email protected]>
To: "Flash Coders List" <[email protected]>
Sent: Saturday, May 09, 2009 3:28 PM
Subject: Re: [Flashcoders] Try... catch......


Hi Hans,

Thanks for the code :) I have one more question for the script below....

1. if catch the TypeError, does the last line => return ball will execute?

Thanks a lot


public function myBall( value:int ):Ball
     {
          var ball:Ball = null;

         try{

                //my code goes here.....
             } catch (e:TypeError ){

         }
     return ball;
}



On Sat, May 9, 2009 at 2:21 AM, Hans Wichman <[email protected]
wrote:

Hi,
yup

either
public function myBall( value:int ):Ball
      {
          var ball:Ball

          try{

                 //my code goes here.....
                 return ball;
             } catch (e:TypeError ){
                  trace("Whoops");
          }
          return null;
      }

or

public function myBall( value:int ):Ball
      {
           var ball:Ball = null;

          try{

                 //my code goes here.....
              } catch (e:TypeError ){

          }
      return ball;
 }
There are more possibilities, some of which are better practice than
others,
but in such a small method, I wouldn't make to much of a fuss about it.

regards,
JC


On Sat, May 9, 2009 at 3:22 AM, ACE Flash <[email protected]> wrote:

> Hi there,
>
> I am trying to add try block in my code, how can I deal it with return
> function?
>
> If the code without any problems, I'd like to return "ball" , > otherwise
I'd
> like to EXIT or return null.
>
> Thanks
>
> -------------------------
>
>  public function myBall( value:int ):Ball
>        {
>            var ball:Ball
>
>            try{
>
>                   //my code goes here.....
>                   return ball;
>               } catch (e:TypeError ){
>
>            }
>
>       // shall I add => return null here?
>        }
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to