Search code examples
datejs

Why does the validateMonth method of the datejs api throw an exception and return a boolean?


Why does the validateMonth method of the datejs api throw an exception and return a boolean?

Date.validateMonth(14)

Will throw the following error.

Timestamp: 12/21/2012 10:56:16 AM Error: RangeError: 14 is not a valid value for months. Source File: http://localhost:18103/soart/scripts/date.js Line: 21

To me this doesn't make sense. It should return a boolean without throwing the exception. The purpose of the method is to check for a valid month after all. Am I missing something?


Solution

  • According to the DateJs documentation it's doing exactly what is expected. To counter this, you could wrap the check in a try catch statement.

    It might throw the exception so that it can return a meaningful error depending on what's been entered. So if a string is passed you get a different error message. In your case, the actual error message is just

    14 is not a valid value for months