Just wanted to write a quick post on the idea of a an "Expected Exception" that can sometimes arise in PowerShell. Take the code below, which simply checks to see if a given SPSite exists on a certain URL:

#Checks if Site Exists - return (true|false)
function CheckSiteExists($url)
{
$site = Get-SPSite $url
if($site -eq $null)
{
$error.RemoveAt(0)
return $false
}
else
{
return $true
}
}

You'll notice the $error.RemoveAt(0) line, which removes the most recent exception thrown in PowerShell. We do this is because if Get-SPSite cannot find a site associated with the passed URL, it will throw an exception. However, since this function is simply used for validation purposes, this exception is expected if the site does not exist. In reality, there are times where you'd want to check if a site exists before acting on it (or creating it), and you may actually be expecting this function to return false. If the script that calls this function treats all exceptions as failures, it will misinterpret this case as a failure when it was actually desired behavior. In my opinion, PowerShell should not throw exceptions in their 'Get' cmdlets that return empty sets.

The code above is not best practice. It treats all exceptions equally, removing them automatically if $site -eq $null. A better way to handle this phenomenon may be to use a try/catch block, or build a more sophisticated error handler to determine if the exception is relevant or not.