Page 1 of 1

Try ... catch statement

Posted: Thu Jun 11, 2009 2:34 am
by dejvos
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello,

I have a problem with "try ... catch" statement in Javascript. I' m not sure if I have an error inside a code or I don't understand this statement well. Do you have any idea where is the mistake?
I' ve used framework mootools-1.2.1-core-jm.js.

Code: Select all

 
var Player = new Class(                                     
{                                                           
    initialize: function(dSpot,dDuration){                  
        /**                                                 
        *       @var string DEFAULT_SPOT Defaultni spot     
        */                                                  
        DEFAULT_SPOT = "default.html";                      
 
        /**
        *       @var int DEFAULT_DURATION Defaultni nastaveni casu;
        */                                                         
        DEFAULT_DURATION = 1;                                      
 
        /**
        *       @var int DEFAULT_PRELOAD
        */                              
        DEFAULT_PRELOAD = 1;            
 
        this.preload = DEFAULT_PRELOAD;
        this.aSpot = null; //aktualni spot
        this.nSpot = null; //nasledujici spot
 
        if(!dSpot)
            currentSpot = DEFAULT_SPOT;
        else                           
            currentSpot = dSpot;       
                                       
        if(!dDuration)                 
            duration = DEFAULT_DURATION;
        else                            
            duration = dDuration;       
 
        this.nSpot = this.initSpot(currentSpot,duration);
        this.show();                                     
    },                                                                                                                                                                              
                                                                                                                                                                                    
    /**                                                                                                                                                                             
    *   Nacte zadany spot;                                                                                                                                                          
    */                                                                                                                                                                              
    initSpot: function(spot,duration){                                                                                                                                              
        try {
            r = new Request.HTML({
                url: spot,
 
                onSuccess: function(tree, elements, html){
                    alert('Success');
                    throw "Error";
                },
 
                onFailure: function(xhr){
                    alert('Failure');
                }
            });
            r.send();
        }catch(e){
            alert(e);
        }
        //alert(PlayerException.PLAYER_REQUEST_ERROR);
    },
 
    show: function(){
 
    },
 
    buildSpot: function(){
        alert('dsad');
    }
}
);
 
var PlayerException = new Class(
{
 
    initialize: function(result,content){
        this.result = result;
        this.content = content;
    }
 
}
);
 
 
Thanks for ANY suggestion.


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Try ... catch statement

Posted: Thu Jun 11, 2009 3:58 am
by onion2k
Have you tried running it in Firefox with Firebug installed? It's by far the best way to track down Javascript errors.

Re: Try ... catch statement

Posted: Thu Jun 11, 2009 4:25 am
by dejvos
Yep, I' ve tried it. No mistakes. The problem is only in the try ... catch statement. The rest of the script is done well. I'm not sure, but I thing that problem should be in the asynchronous executing of the Request.HTML(...).send(); and the rest of the script.

I don't know how does the sending of messages works in the Javascript. This issue could be done by another construction ... may be by some kind of listener. So, maybe I' ve used a bad construction ... I don't know.

Re: Try ... catch statement

Posted: Thu Jun 11, 2009 4:31 am
by VladSun
I don't think you can do this. You have an asynchronous call to onSucces, so you will throw the Exception somewhere else. Try a "scope" call:

[js] initSpot: function(spot,duration){                                                                                                                                                      var self = this;            r = new Request.HTML({                url: spot,                 onSuccess: function(tree, elements, html){                    alert('Success');                    self.spotLoaded()                },                 onFailure: function(xhr){                    alert('Failure');                }            });            r.send();        }        //alert(PlayerException.PLAYER_REQUEST_ERROR);    }, spotLoaded: function (){   alert('spotLoaded');}[/js]

Re: Try ... catch statement

Posted: Thu Jun 11, 2009 4:51 am
by dejvos
Yes, this works! Thanks.