0

For example if use decodeURI('%C4%97%') it fires and error (yes, it is an error, specially for test):

URIError: malformed URI sequence ...('textarea#encode-url-result').val(decodeURI(jQuery('input#encode-url-input').va...

And even if i put it in try-catch it still fires fatally. Is there a way to catch it and show alert?

UPDATE:

Here is my code and i still get error in console

try{
    jQuery('a#encode-url-encode, a#encode-url-decode').click(function(){
        if(jQuery('input#encode-url-input').val().length == 0)
            showCustomAlert('<strong>Warning!</strong> Please enter value.');

        var result = null;

        if(jQuery(this).attr('id') == 'encode-url-encode')
            result = encodeURI(jQuery('input#encode-url-input').val());
        else if(jQuery(this).attr('id') == 'encode-url-decode')
            result = decodeURI(jQuery('input#encode-url-input').val());

        jQuery('textarea#encode-url-result').val(result);
    });
}
catch(e){
    alert(e);
}
user1692333
  • 2,461
  • 5
  • 32
  • 64
  • Wrong. http://jsfiddle.net/SLaks/md7s7/ – SLaks Apr 12 '13 at 20:37
  • How did you put it in try-catch? Try-catch doesn't work if it wraps async callback passing. – Esailija Apr 12 '13 at 20:37
  • That catch will not catch anything in the callback function. You are declaring the function there, not calling it. Easiest fix is to move the try/catch inside the callback function. – Matt Greer Apr 12 '13 at 20:48
  • @MattGreer it does work when the callback is called synchronously. – Esailija Apr 12 '13 at 20:49
  • @Esailija No it wouldn't, that function won't get called until the url is clicked, and at that point the try/catch is long gone: http://jsfiddle.net/pgevj/ – Matt Greer Apr 12 '13 at 20:57
  • @MattGreer your example is async, I am saying it works for sync callbacks and in fact my first comment already said it doesn't work for async callbacks. – Esailija Apr 12 '13 at 20:58
  • @Esailija can you given an example? – Matt Greer Apr 12 '13 at 20:58
  • @MattGreer http://jsfiddle.net/pgevj/1/ – Esailija Apr 12 '13 at 20:59
  • @Esailija That's not a callback, but ok, I see what you are saying. – Matt Greer Apr 12 '13 at 21:01
  • @MattGreer of course it's a [callback](http://en.wikipedia.org/wiki/Callback_\(computer_programming\)). I am not surprised to see C# in your profile - "callback must be async" is a C# idiosyncracy and is not correct. – Esailija Apr 12 '13 at 21:02

4 Answers4

6

This works fine:

try {
    decodeURI('%C4%97%')
} catch (ex) {
    alert("ERROR DECODING URI");
}

DEMO: http://jsfiddle.net/P6EBN/

EDIT:

From the looks of your error message, you're trying to set a textarea's value with jQuery.

Try something like this:

var newVal = "";
var toDecode = jQuery('input#encode-url-input').val();
try {
    newVal = decodeURI(toDecode);
} catch (ex) {
    // alert("ERROR DECODING URI");
}
jQuery('textarea#encode-url-result').val(newVal);

Just trying to split it up so you can target specifically the decoding.

Also, the use of a tagName in front of an id selector is unnecessary. Just use these selectors:

jQuery("#encode-url-input")
// and
jQuery("#encode-url-result")
Ian
  • 50,146
  • 13
  • 101
  • 111
  • @user1692333 You can't just wrap a whole block of *asynchronous* code with a `try/catch`. You need to target the specific operation...specifically where you're calling `decodeURI`. I already had that in my answer – Ian Apr 12 '13 at 20:51
2

Try-catch doesn't work if it wraps async callback passing.

jQuery('a#encode-url-encode, a#encode-url-decode').click(function() {
    if (jQuery('input#encode-url-input').val().length == 0) showCustomAlert('<strong>Warning!</strong> Please enter value.');
    var result = null;
    try { //It needs to be here.
        if (jQuery(this).attr('id') == 'encode-url-encode') result = decodeURI(encodeURI(jQuery('input#encode-url-input').val()));
        else if (jQuery(this).attr('id') == 'encode-url-decode') result = decodeURI(decodeURI(jQuery('input#encode-url-input').val()));
    } catch (e) {
        //handle
    }
    jQuery('textarea#encode-url-result').val(result);
});
Esailija
  • 138,174
  • 23
  • 272
  • 326
1
try {
    var x = decodeURIComponent('%C4%97%');
} catch (ex) {
    console.log("ERROR DECODING URI: " + ex.message);
}
HopeNick
  • 244
  • 3
  • 11
0
try
{
    var x = decodeURI('%C4%97%');
}
catch(e)
{
    alert(e);
}
Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75