1

I'm trying this to catch an exception but it just doesn't work rather just shows me error in the script editor I'm running the script from:-

Path I mentioned in the script "\server\abc" doesn't really exist, so it should catch it as an exception which it is not. HELP HELP

Try
{

Get-ChildItem -Path "\\server\abc"

}
Catch
{

 Write-Host "error"

}

1 Answer 1

8

You need to set the erroraction to STOP for the error to be terminating - only terminating errors are raised to a catch block.

Try
{

Get-ChildItem -Path "\\server\abc" -ErrorAction Stop

}
Catch
{

 Write-Host "error"

}
Sign up to request clarification or add additional context in comments.

1 Comment

use $ErrorActionPreference = "Stop" more ref: blogs.msdn.com/b/kebab/archive/2013/06/09/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.