Why isn't my try catch error not showing up?












1















I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.



This would work:



<!DOCTYPE html>
<html>
<body>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>

<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>

</body>
</html>


But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/










share|improve this question























  • Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.

    – Caleb Adams
    Jan 2 at 19:37











  • Your code works fine with codepen: codepen.io/anon/pen/mapjKQ

    – Armel
    Jan 2 at 19:39
















1















I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.



This would work:



<!DOCTYPE html>
<html>
<body>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>

<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>

</body>
</html>


But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/










share|improve this question























  • Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.

    – Caleb Adams
    Jan 2 at 19:37











  • Your code works fine with codepen: codepen.io/anon/pen/mapjKQ

    – Armel
    Jan 2 at 19:39














1












1








1








I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.



This would work:



<!DOCTYPE html>
<html>
<body>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>

<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>

</body>
</html>


But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/










share|improve this question














I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.



This would work:



<!DOCTYPE html>
<html>
<body>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>

<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>

</body>
</html>


But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/







javascript html try-catch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 19:17









k0rruptedk0rrupted

104




104













  • Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.

    – Caleb Adams
    Jan 2 at 19:37











  • Your code works fine with codepen: codepen.io/anon/pen/mapjKQ

    – Armel
    Jan 2 at 19:39



















  • Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.

    – Caleb Adams
    Jan 2 at 19:37











  • Your code works fine with codepen: codepen.io/anon/pen/mapjKQ

    – Armel
    Jan 2 at 19:39

















Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.

– Caleb Adams
Jan 2 at 19:37





Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.

– Caleb Adams
Jan 2 at 19:37













Your code works fine with codepen: codepen.io/anon/pen/mapjKQ

– Armel
Jan 2 at 19:39





Your code works fine with codepen: codepen.io/anon/pen/mapjKQ

– Armel
Jan 2 at 19:39












1 Answer
1






active

oldest

votes


















2














It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() { to make it global, it will function as expected.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011979%2fwhy-isnt-my-try-catch-error-not-showing-up%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() { to make it global, it will function as expected.






    share|improve this answer




























      2














      It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() { to make it global, it will function as expected.






      share|improve this answer


























        2












        2








        2







        It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() { to make it global, it will function as expected.






        share|improve this answer













        It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() { to make it global, it will function as expected.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 19:26









        jmcgrizjmcgriz

        2,3961410




        2,3961410
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011979%2fwhy-isnt-my-try-catch-error-not-showing-up%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas