I am running the following power shell script using power shell console:
$ob1 = "nonexistingclass"
$a = new-object $ob1
Write-Output "Created new object"
Write-Output "End"
This prints the error. And then continues and prints the "Created new object" and "End". So I am assuming this is a non terminating error.
But if I put try catch block around the new-object as follows:
$ob1 = "nonexistingclass"
try
{
$a = new-object $ob1
Write-Output "Created new object"
}
catch
{
Write-Error "Exception Message: $($_.Exception.Message)"
}
Write-Output "End"
In this case catch block is hit and it writes the exception message.
My questions are:
- Is this a non terminating error? It looks like a non terminating error as the execution continues after the error (when there is no try catch block).
- If this is non terminating error why catch block is getting hit when I add the try catch block? My understanding is catch block will be executed only for terminating errors.
- Even when I invoke new-object in the first example (without try catch block) with -ErrorAction Stop, it still continues execution and prints the last 2 line. When I use -ErrorAction stop I am expecting that it should throw a terminating exception and the script execution should stop. Is my understanding wrong?