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