0

This is my first time working with JavaScript exceptions and exception handling in general.

OK I have the following simple code.

function getMonth( month ) {

    month = month -1;
    var months = [
                    "January",
                    "February",
                    "March",
                    "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December"
                ];

    if( month != null ) {
        if( months.indexOf( month ) ) {
            return months[month];
        } else {
            throw "Invalid number for month";
        }
    } else {
        throw "No month provided";
    }
}

Basically it first checks to see if an argument was provided to the function and if not, throw and exception. Second, it checks to see if the provided number matches a valid month number and if not throw an exception.

I ran it as follows:

try {
    month = getMonth();
} catch( e ) {
    console.log( e );
}

Console does not log the exception, "No month provided". Why is this? I am currently reading about exceptions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#Exception_Handling_Statements

2 Answers 2

1

Thats a tricky one. The Problem here is: Your Code actually never reaches the throw Statements, but runs through and returns normally. The reason is a bit "JavaScriptish":

On calling the function, the variable month is introduced (as it is within the arguments), but to this point its value is undefined. The statement month -1; then is the same as undefined - 1 which returns an Illegal Number or NaN. (would be better if it actually threw, I agree).

Your first if(month != null) now resolves to true as month = NaN and NaN != null. The second condition returns true because Array.indexOf does actually return -1 if the given value is NOT within the array. And -1 is a valid value and thus resolves to true.

I agree again - thats all a bit counterintuitive. But hey - welcome to JavaScript. It has it's good sides, i promise ;)

Little tip: Try all those conditions and statements i posted in the console of your browser, then you gonna understand it better.

Sign up to request clarification or add additional context in comments.

Comments

0

It's because the month variable is undefined and it not equals null.

try {
    month = getMonth();
    alert(month);
} catch( e ) {
    alert(e);
}


function getMonth( month ) {

    month = month -1;
    var months = [
                    "January",
                    "February",
                    "March",
                    "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December"
                ];

    if( month != null ) {
        if( months.indexOf( month ) ) {
            return months[month];
        } else {
            throw "Invalid number for month";
        }
    } else {
        throw "No month provided";
    }
}

For more information read this article

2 Comments

Thanks. I changed the conditional to if( month != undefined ) it still does not throw the exception "No month provided". Why?
Is the code you provided in the question exactly the same as the code you are testing? Anyway, try to change it to if ((month != undefined) && (month != null))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.