Try ... catch statement

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Try ... catch statement

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Try ... catch statement

Post by onion2k »

Have you tried running it in Firefox with Firebug installed? It's by far the best way to track down Javascript errors.
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: Try ... catch statement

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Try ... catch statement

Post 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]
There are 10 types of people in this world, those who understand binary and those who don't
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: Try ... catch statement

Post by dejvos »

Yes, this works! Thanks.
Post Reply