AlbatrossCafe AlbatrossCafe - 1 year ago 64
jQuery Question

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

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 () {
console.log("This won't show");

$("#should-error").click(function () {
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
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
    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 -

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
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
to circumvent the error or to somehow check if the variable exists before I use
. I know and want the error to happen, I just want to prevent its display. I hope that makes this more clear.

Answer Source

If I had a statement like:


With some error (in this case, it is a double right parenthsis), I was hoping that, without modifying the code logic in any way, I could prevent just this single error from being thrown to the console. If it was in a function like:

function ()

...console.log would not be executed, and the function would exit.

Ideally, I was thinking there would be something like:

function ()
    Console.ToggleErrorDisplay("off");    //fake function

...and the error would not display in the web console but the function/code would still behave like it did.

Based on my fairly extensive research, I have found this to be not possible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download