7

This time I don't have any problem but just for curiosity I want to know how many exception are there in JavaScript.

For example I am using following code:

<script type="text/javascript">
    var x;
    try{
        x = 1 / 0;
        alert(x); // output: Infinity. FYI: JavaScript has Infinity property and it's value is 1.7976931348623157E+10308
        alert(funCreate());
    }
    catch(obj)
    {
        alert(obj.fileName);
        alert(obj.lineNumber);
        alert(obj.message); // output: funCreate is not defined
        alert(obj.name); // output: ReferenceError
        alert(obj.stack);
    };
</script>

here, ReferenceError is like an exception type. So if it is treated as an exception type, then can we handle the exception by type? like we all do in other programming language. see link.

Thanks...

7
  • 2
    Infinity is Infinity, it is not 1.7976931348623157E+308
    – kennytm
    Sep 7, 2010 at 8:10
  • 1
    @KennyTM - Try this: alert(1.7976931348623157e+309). It shouldn't be surprising. How do you express actual Infinity in double-precision floating point number? Well, you can't. What do you show when you run out of bits to express a number? As far as JS is concerned, any value beyond 1.7976931348623157e+308 is 'Infinity'. JS shouldn't be your first choice for numerical accuracy.
    – Andrew
    Sep 7, 2010 at 9:33
  • yes, Infinity is displayed when a number exceeds the upper limit of the floating point numbers, which is 1.7976931348623157E+10308.
    – Vikas
    Sep 7, 2010 at 9:51
  • 1
    And amazing thing is you can write infinite for loop like for(i=0;i<=Infinity; i++){}
    – Vikas
    Sep 7, 2010 at 10:06
  • 1
    @Andrew: 1.7976931348623157e+309 becomes Infinity because the value is too big, but it does not mean JavaScript has Infinity property and it's value is 1.7976931348623157E+10308 quoted from OP's comment. In IEEE 754 "Infinity" itself already is a special value, it has no decimal representations.
    – kennytm
    Sep 7, 2010 at 13:03

3 Answers 3

13

I believe there are six exception types in JS:

  1. EvalError (errors produced from an eval();
  2. RangeError (produced when using a number that is out of the range for where it is being used -- I've actually never seen this one in real life and I can't seem to produce it now);
  3. ReferenceError (produced when trying to access a non-existent member of an object by name);
  4. SyntaxError;
  5. TypeError (when a method was expecting a value of a different type); and
  6. URIError (produced when trying to create or decode a URI).

The problem, unfortunately, is that these exception types are not supported universally -- the two big hold-outs being Safari and Opera. As well, you'll find that lineNumber and fileName work only on Firefox (maybe others?) and the strings you get back for message will vary from browser to browser. So in practice, it's best to avoid using these at all and manage your exception handling manually and more directly.

6
  • Yes, lineNumber and fileName work only on Firefox! But The same code gives the different value of obj.name in IE! In IE, it gives TypeError!
    – Vikas
    Sep 7, 2010 at 9:14
  • I've often thought that the reason that there's so much diversity in error handling messages is that so few developers actually do error handling in JS. Aside from the big JS frameworks, I can't remember the last time I saw throw being used in someone's code. Compare that to C# or Java, where more than half your code can end up just handling exceptions. All in all, I prefer JS because of that, actually.
    – Andrew
    Sep 7, 2010 at 10:12
  • I tried to throw custom exception by writing if(1==1) throw ("My error"); But owr catch handler accept the object type. So I pass an object by creating it with Literal Notation, if(1==1) throw {name:"custom", message:"My error"};
    – Vikas
    Sep 7, 2010 at 10:44
  • That's actually a really good solution. I'd not thought of doing that.
    – Andrew
    Sep 7, 2010 at 17:15
  • 1
    RangeError example = new Array(5.5)
    – Lam Chau
    Sep 19, 2013 at 0:14
4

There's no such syntax in javascript, but you can implement similar thing easily:

var x;
try{
    x = 1 / 0;
    alert(x); // output: Infinity. FYI: JavaScript has Infinity property and it's value is 1.7976931348623157E+10308
    alert(funCreate());
}
catch(obj)
{
    switch(obj.name) {
        case 'ReferenceError':
            alert(obj.fileName);
            alert(obj.lineNumber);
            alert(obj.message); // output: funCreate is not defined
            alert(obj.name); // output: ReferenceError
            alert(obj.stack);
        break;
        case 'AnotherError':
            //do other things
        break;

        default:
           //other stuff
    }
};
3

You can throw anything in JavaScript, so there is no list of possible exceptions. If you would like to see all properties of the default exception object, i would recommend firebug's console.log()-command.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.