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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.