Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, February 1, 2011 7:24 PM
Ok, so I try the following script:
try { Get-Process junkProcess }
catch { echo $_.Exception.Gettype().FullName }
And it doesn't catch the error. Why?
A
Adrian
All replies (8)
Tuesday, February 1, 2011 7:58 PM ✅Answered | 2 votes
It may not be a terminating error. Try this:
try { Get-Process vmwindow -ea stop }
catch { echo $_.Exception.Gettype().FullName }
Also, can you give an example of what happens when you run your command with try/Catch and the message afterwards?
Are you running PowerShell V2 or V1? (Run $psversiontable and post results)
Tuesday, February 1, 2011 7:58 PM ✅Answered | 1 vote
Use ErrorAction...
try { Get-Process vmwindow -ErrorAction 'Stop' }
catch { echo $_.Exception.Gettype().FullName }
Tuesday, February 1, 2011 8:27 PM ✅Answered
Try setting your ErrorActionPreference to stop:
PS C:\> $ErrorActionPreference = 'STOP'
Tuesday, February 1, 2011 7:33 PM
maybe you actually have a process called junkprocess :)
try {get-process junk} catch {echo $_.exception.gettype().fullname}
that worked fine for me, returns
Microsoft.PowerShell.Commands.ProcessCommandException
what does it return?
Tuesday, February 1, 2011 8:48 PM
Please use the Powershell onlinehelp:
get-help about_Try_Catch_Finally
and you will get some exapmles.
The global syntax is:
try {<command>}
catch [[<errortyp>][]] {<commands>}
If you want to read about the possible errorcods of a command please read http://msdn.microsoft.com
regards Thomas Paetzold visit my blog on: http://susu42.wordpress.com
Tuesday, February 1, 2011 9:25 PM
PS C:\Users\SL510> try { Get-Process vmwindow }
catch { echo $_.Exception.Gettype().FullName }
Get-Process : Cannot find a process with the name "vmwindow". Verify the process name and call the cmdlet again.
At line:1 char:18
- try { Get-Process <<<< vmwindow }
+ CategoryInfo : ObjectNotFound: (vmwindow:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
Using -ea stop fixed the problem. Thanks.
A
Adrian
Wednesday, February 2, 2011 3:23 AM
Interesting about this error action stuff. Will have to look into this in more detail. I thought I had posted a thank you and marked at least one as an answer. Seems something in interfering with my posting. :(
Anyway, thanks all.
A
Adrian
Wednesday, February 2, 2011 3:25 AM
Yeah, something screwy is happening to the forums. Anyone else having any issues? :( I just tried to mark some and I got an error. I refreshed and it was already marked as well as my reply showed up. I had refreshed the page several times before too. And it isn't cached since I've never been on this page from this computer before. Hmmmmmmm...
A
Adrian