Search code examples
javascriptthrow

Is there an else statement for throw exceptions?


I'm new to JavaScript, but not new to programming/scripting in general and I have a query about throw exceptions.

So, what I want to know is there a sort of else statement for throw exceptions.

E.g

// More code above
try
{
    if(i=="something") {
        throw "'i' equals 'something'."
    }
    else
    {
        throw "'i' doesn't equal 'something'."
    }
}
// More code here

Is something like that possible?


Solution

  • You are probably looking for the finally keyword

    try
    {
        if(i=="something") {
            throw "'i' equals 'something'."
        }
        else
        {
            throw "'i' doesn't equal 'something'."
        }
    } catch( ex ) {
        // your exception handling code
    } finally {
        // Statements that are executed after the try statement completes.
    }
    

    Demo: http://jsfiddle.net/7y6Hz/