Exceptions In JavaScript

Discussion in 'JavaScript and AJAX' started by pradeep, Jun 27, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    Introduction



    The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages.

    Use the throw statement to throw an exception. When you throw an exception, expression specifies the value of the exception. Each of the following throws an exception:

    Code:
     throw "Error2"; // generates an exception with a string value 
        throw 42; // generates an exception with the value 42 
        throw true; // generates an exception with the value true 

    Syntax



    throw(exception)

    The exception can be a string, integer, Boolean or an object.

    Note: that throw is written in lowercase letters. Using uppercase letters will generate a JavaScript error!

    Example



    Code:
        function userException(msg,errCode)
        {
            this.description = msg;
            this.err = errCode;
        }
        
        function divide(operand,divisor)
        {
            if(divisor==0)
            {
                var ex = new userException('Division by zero is not possible');
                throw ex;
            }
        
            return operand/divisor;
        }
        
        try
        {
            var result = divide(123,0); // trying to divide by zero
        }
        catch(err)
        {
            alert('Exception: '+err.description+' ('+err.err+')');
        }
        
     
  2. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    Theres also another keyword "throws" which many times novice java geeks get confused with.
    "throws" clause allows any method to throw exception to the caller. In the sense when it is used with any method, that method will not catch the exception and inturn has no exception handler. So it passes back to where it came from !
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Well, this is JavaScript not Java, both are very much different!
     
  4. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    oopss...sorry posted without reading the title anyways thanks for your information.
     
  5. LMatta

    LMatta New Member

    Joined:
    Jun 26, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Which is the best book for beginners to read Javascript
     
  6. LMatta

    LMatta New Member

    Joined:
    Jun 26, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    This Article is Awesome.... Able to understand it very clearly..
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Well I don't know about any such book, check the resources listed in the Javascript forums here!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice