Is it possible to prevent a specific JavaScript error from being written to the console?
Asked Answered
E

3

7

Hypothetically - let's say I have some JavaScript to handle the clicks of three different buttons:

$("#working").click(function () {
    alert("Seth Rollins is the greatest champion of all time.");
    console.log("WWE World Heavyweight Champion");
});

$("#fix-this-error").click(function () {
    alert(this_should_not_show_up_in_the_console);
    console.log("This won't show");
});

$("#should-error").click(function () {
    alert(oipewavqn2389tvb3t8ahwleuhtvab3iwuthabvlewiuweaqhaoiuhva98r3b2);
    console.log("This won't show either");
});

The first one will work as it alerts a string, and it will log to the console the message that is written after the alert.

The second and third functions will not work because they are trying to alert variables that are not defined. Their subsequent console.logs will not output anything.

My Question: Is it possible to prevent the error from the second function from outputting to the console while maintaining the following properties?:

  • The first function should work as intended
  • The second function's subsequent console.log still should not execute
  • The third function (and any other functions) should still output their errors

Edit: Here is a fiddle to play around in - https://jsfiddle.net/5m40LLmm/2/

SUPER EDIT: I don't want the execution of logic in the second function to really change. I want the error to be thrown, and the .click() handler should exit before it reaches console.log as it does now. I just want to prevent the error from being shown. I don't want to use try and catch to circumvent the error or to somehow check if the variable exists before I use alert(). I know and want the error to happen, I just want to prevent its display. I hope that makes this more clear.

Eyot answered 15/7, 2015 at 16:58 Comment(3)
try catchBoding
Why don't you want to use try catch? It's exactly the feature you are looking for!Tarnetgaronne
Wow I am looking at this question 2 years later and I have NO IDEA what the **** I was thinking. What could I have possibly been trying to do? It makes no sense to me and I am the one that wrote it. lolEyot
S
2

When you run this, the console.log function is never actually called. The alert function fails, logs that it failed, and the click listener function exits. console.log() is never reached. This means you just need to stop the alert from showing an error. This is where try catch statements are helpful.

$("#fix-this-error").click(function () {
    try {
        alert(this_should_not_show_up_in_the_console);
        console.log("This won't show");
    } catch (e) {
        // Code jumps here when `alert()` fails.
        // `e` stores information about the error
        // If you don't want to put everything in the try/catch,
        // you can stop the function from continuing execution with
        return;
    }
});
Sheena answered 15/7, 2015 at 17:16 Comment(4)
You are right in that alert fails and click exits. I want this functionality.(No logic should progress after the error) I just don't want the error to show. I'll update my question to be more clear as people seem confusedEyot
That's what this doesSheena
yeah but it doesn't break the click function. You see in this fiddle: jsfiddle.net/5m40LLmm/3 that if I insert console.log() after the try/catch, it is still executed. I don't want anything else in the flick function executed after the error occurs. I am pretty sure what I am asking for is nearly impossible lolEyot
Updated. Using the return keyword will stop the function.Sheena
B
4

Use try catch

$("#fix-this-error").click(function () {
    try {
        alert(this_should_not_show_up_in_the_console);
        console.log("This won't show");
    } catch() {}
});

or check to see if the variable is defined before you try to use it.

Boding answered 15/7, 2015 at 17:4 Comment(2)
sorry, please refer to my "super edit" in the question. I don't want to prevent the error from happening, I just want to prevent it's display in the consoleEyot
great, that is my question. If you have any knowledge that proves you can 100% not do this I would like to know.Eyot
S
2

When you run this, the console.log function is never actually called. The alert function fails, logs that it failed, and the click listener function exits. console.log() is never reached. This means you just need to stop the alert from showing an error. This is where try catch statements are helpful.

$("#fix-this-error").click(function () {
    try {
        alert(this_should_not_show_up_in_the_console);
        console.log("This won't show");
    } catch (e) {
        // Code jumps here when `alert()` fails.
        // `e` stores information about the error
        // If you don't want to put everything in the try/catch,
        // you can stop the function from continuing execution with
        return;
    }
});
Sheena answered 15/7, 2015 at 17:16 Comment(4)
You are right in that alert fails and click exits. I want this functionality.(No logic should progress after the error) I just don't want the error to show. I'll update my question to be more clear as people seem confusedEyot
That's what this doesSheena
yeah but it doesn't break the click function. You see in this fiddle: jsfiddle.net/5m40LLmm/3 that if I insert console.log() after the try/catch, it is still executed. I don't want anything else in the flick function executed after the error occurs. I am pretty sure what I am asking for is nearly impossible lolEyot
Updated. Using the return keyword will stop the function.Sheena
D
0

Use the typeof keyword to check if a particular variable is defined or not.

typeof documentation

$("#fix-this-error").click(function () {
    if (typeof this_should_not_show_up_in_the_console !== "undefined")
    {
        alert(this_should_not_show_up_in_the_console);
        console.log("This won't show");
    }
});
Descendible answered 15/7, 2015 at 17:4 Comment(1)
sorry, please refer to my "super edit" in the question. I don't want to prevent the error from happening, I just want to prevent it's display in the consoleEyot

© 2022 - 2024 — McMap. All rights reserved.