For some reason this code gives me an uncaught exception error. It seems the catch block is not catching the error. Are try catch blocks scoped in such a way that I cannot throw an error in a nested function, and then expect it to be caught by a catch statement scoped higher up the chain? Some of the sensitive data with in the application that i'm working in has been removed, but it expected that leadInfo[ 0 / 1] would be a 32 character alpha numeric string that I pull from URL parameters.
The underlying issue here is with my AJAX call returning an error from the API and that error not being handled properly within the application. Hence the need for the throw statement. The AJAX call completes fine, and returns a JSON object that does not contain the email address as a property, so I need to handle that in a way that changes the page to reflect that.
jQuery(document).ready(function(){ try { url = "http://api.com/api/v1/lead/" + leadInfo[1] jQuery.ajax({ type: 'GET', contentType: 'application/json', url: url, dataType : 'jsonp', success: function (result) { result = jQuery.parseJSON(result); if(!result.data.email){ throw ('New exception'); } console.log(result); jQuery('.email').html(result.data.email); } }); jQuery('.surveryButton').click(function(){ window.location.replace("http://" + pgInventory.host + pgInventory.path + leadInfo[0] + "&curLeadId=" + leadInfo[1] + "&curViewedPages=0"); }); } catch(err) { jQuery('.email').html('your e-mail address'); jQuery('#arrowContent').remove(); } });
When you have a try/catch block in Javascript, the catch block will take any error that can happen in try block. The keyword throw is used to throw a error to the superior scope (who call the function for sample) passing the error on it (exception) that will be taken by the catch block.
Yes, we can catch an error. The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the throw statement.
Hi Shola, yes you can use try / catch without a throw.
So, try... catch can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”. That's because the function itself is executed later, when the engine has already left the try...
The reason why your try catch
block is failing is because an ajax request is asynchronous. The try catch block will execute before the Ajax call and send the request itself, but the error is thrown when the result is returned, AT A LATER POINT IN TIME.
When the try catch
block is executed, there is no error. When the error is thrown, there is no try catch
. If you need try catch
for ajax requests, always put ajax try catch
blocks inside the success
callback, NEVER outside of it.
Here's how you should do it:
success: function (result) { try { result = jQuery.parseJSON(result); if (!result.data.email) { throw ('New exception'); } console.log(result); jQuery('.email').html(result.data.email); } catch (exception) { console.error("bla"); }; }
Due to the asynchronous nature of the callback methods in javascript, the context of the function throwing the error is different compared to the original one. You should do this way:
success: function (result) { try { result = jQuery.parseJSON(result); if(!result.data.email){ throw ('New exception'); } console.log(result); jQuery('.email').html(result.data.email); } catch(err) { // Dealing with the error } }
I would suggest you to have a look at this excellent article about the (very particular) contexts, closures and bindings in Javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With