Page 1 of 1

Unexpected Exception thrown, even after check.

Posted: Tue Sep 29, 2009 12:46 pm
by jeff00seattle
I have the following code whereby an Exception is thrown even after check.

In this code, if function setupSearchBar() is not defined, then do not call this function:

Code: Select all

function setupSearchOnLoad()
{
  try
  {
    if (setupSearchBar != undefined)
    {
      setupSearchBar();
    }
  }
  catch (ex)
  {
    alert("setupSearchOnLoad: Exception: "+ex.name+": "+ex.message);
  }
}
However, what occurs is an Exception is thrown from the if conditional statement because setupSearchBar is not defined:

Exception.error: ReferenceError
Exception.message: "setupSearchBar is not defined"


Why would this occur?

Jeff in Seattle

Re: Unexpected Exception thrown, even after check.

Posted: Tue Sep 29, 2009 2:29 pm
by kaszu
Because you are trying to access undefined variables value. Following will work:

Code: Select all

if (typeof setupSearchBar == 'function')