UPDATED: Well done Rick for spotting this one! It is considered bad practice to return from a finally block – the behavior of the code is unpredictable, based on the route the code takes and whether an exception is thrown or not. Here’s some further reading on the subject. If an exception must be thrown, the return should happen outside of the try…catch. If the function must return a value, the catch should return a value, for example -1, while suppressing (not throwing) the exception.
Really quick one today! I found this in loads of code at my latest project – can you identify the problem?
function CodeChallenge10()
{
// Declare variables
var boContact, bcContact;
var numContact = 0;
try
{
// Instantiate new BC instance
boContact = TheApplication().GetBusObject("Contact");
bcContact = boContact.GetBusComp("Contact");
with (bcContact)
{
// Create query context
ClearToQuery();
SetViewMode(AllView);
ExecuteQuery(ForwardOnly);
numContact = CountRecords();
return numContact;
}
}
catch(e)
{
// Call the custom LogError function to write error to error log
LogError("Woops, an error has occurred!");
}
finally
{
// Destroy objects
bcContact = null;
boContact = null;
return numContact;
}
}
Answers below, please!






