Search code examples
exceptiontitaniumhttpclientappceleratoronload

Titanium Appcelerator - HTTPClient - onload - custom exception


I am using Titanium Studio (build: 3.4.0.201409261227) and I am trying to catch an exception within the "onload" callback of Ti.Network.HTTPClient.

But that's not possible, I am getting the "Red Screen" with "Application Error" and I am not able to catch the exception.

Anyone an idea?

try {
        var tiHTTPClient = Ti.Network.createHTTPClient({
            onload : function(e) {      
                throw 'EXCEPTION';              
            }
        });     
        tiHTTPClient.open( 'GET', 'http://www.google.com' );
        tiHTTPClient.send();
    }
    catch( e ) {
        alert( 'E: ' + e.message ); 
    }

Outside the "onload" function, it's easily possible via:

try {
    throw 'EXCEPTION';              
}
catch( e ) {
    alert( 'E: ' + e.message ); 
}

Solution

  • This might be the correct answer or at least the explanation :

    BTW, the reason your try...catch blog makes no difference is that "invoke" succeeds -- it's an asynchronous call so it just returns control immediately, and you therefore pass right through the try block without any problems. Blockquote

    And one solution could be:

        var exceptionHandling = function( msg ) {
            alert( 'E: ' + msg );
        };
    
        var tiHTTPClient = Ti.Network.createHTTPClient({
            onload : function(e) {  
                exceptionHandling('EXCEPTION');              
            }
        });     
        tiHTTPClient.open( 'GET', 'http://www.google.com' );
        tiHTTPClient.send();