Examining exceptions using 'Try Catch Finally in Powershell

I've read the about_try_catch_finally via powershell help.

I want to incorporate try-catch-finally in my Powershell-scripting. In example i tried the code attached. dividebyzero is caught (example via google), and then i tried system.io.FileNotFound (I know i can use test-path, but I'm just examining).  'Finally' will run but the Catch wont catch and run the code.

1) Can any1 explain why
2) I really need to learn exceptions - Any suggestions/links that will give me focus in that process?  


$one=1
$zero=0

try{
 	$one/$zero
}

catch [System.DivideByZeroException]{
 #"The first Catch script block handles System.DivideByZeroException exceptions"
 "Catched! Attempted to divide by zero"
}

catch {
 	"the second Catch script block handles any non-specific exceptions"
}
 
finally {
 	"Ahh Survived the divide by zero error"
	try{
		$hej = Get-Content "asdf.txt"	
	}	
	catch [System.IO.FileNotFoundException]{
		"Catched! File not found muahhahaha"
	}	
	finally {	
		"Phew - survived the File not found"
	}
}

Open in new window

LVL 1
jmateknikAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
It can't catch System.IO.FileNotFoundException because it is never thrown. It is claiming to throw System.Management.Automation.ItemNotFoundException.

The trouble is, I can't make it catch that one either, the block I'm using is below. It is only possible to get this far by defining ErrorAction as stop. It is possible to catch it as System.Exception, but that's hardly detailed error handling.

There are a few blog posts on this topic, this is one of the more recent:

http://outputredirection.blogspot.com/2010/04/powershells-trycatchfinally-and.html

Spawned from this discussion on the newsgroups:

http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.windows.powershell&tid=e0336f47-7516-4390-9e1a-a4d364309777&cat=en_US_3750E87B-4971-4A5C-A537-45F5D7ABBECC&lang=en&cr=US&sloc=&p=1

All in all, not much help :-\

Chris
try{
  $hej = Get-Content "asdf.txt" -ErrorVariable x -ErrorAction Stop
}
catch [System.Management.Automation.ItemNotFoundException] {
  "Caught ItemNotFoundException from Exception: $($_.Exception)"
}
catch [Exception]
{
  "Caught General Exception from Exception: $($_.Exception)"
}
finally {       
  "Phew - survived the File not found"
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmateknikAuthor Commented:
Thanks - its definitely a help.

Funny though - at http://outputredirection.blogspot.com/2010/04/powershells-trycatchfinally-and.html
I found that this works (as opposite to what the author found 9. of april:
try { gci c:\badfolder -ea stop} catch { [System.Management.Automation.ItemNotFoundException] } { "GOT IT"}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.