0

I feel silly asking this, I must be missing something obvious.

My javascript looks as follows...

function onNewItemClick(event)
{
    alert('ello!');
    try
    {
        var xx = 111/0;
    }
    catch(exc)
    {
        alert('fff');
    }
}

alert('ello!') works like it should, but the alert('fff') in the Catch block never gets called. What am I doing wrong here?

1 Answer 1

7

The catch doesn't executes because division by zero doesn't rises an exception, it simply sets you xx variable to Infinity.

To check if a number is finite you can use the isFinite function:

if (!isFinite(xx)) {
  //...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Or to undefined depending on the browser.
Ha ha, that's what happens when a .NET junkie ventures into the javascript world :) Thanks!
@willem: You're welcome!, @jitter: That behavior is pretty much standard (see ECMA-262 Section 11.5.2) is.gd/5clge (PDF)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.