Handling layers of try/catch

Hi again,

I was hoping someone here could help me out with something.

 

Say I have the following, extremely advanced and intricate script running

[pre]

try
{
try
{
try
{
[System.IO.File]::ReadAllText(‘c:\does\not\exist.txt’)
}
catch
{
Write-Host “Level 1.”
}
}
catch
{
Write-Host “Level 2.”
}
}
catch
{
Write-Host “Level 3.”
}
finally
{
Write-Host “It’s the end of the file as we know it. And I feel fine.”
}

[/pre]

 

Now, this performs as expected, but not as desired.

 

Is it possible to have it trickle down through all the layers of catch blocks? To be clear: I want it to fail. I just want to be able to compartmentalize things a little.

 

Thanks!

A quick search brought up among others these hits … they might enlighten you a little bit

https://stackoverflow.com/questions/32522983/powershell-nesting-try-catch-finally-commands

https://social.technet.microsoft.com/forums/scriptcenter/en-US/63005b70-f5b4-430c-9e51-16ed2373e48e/powershell-nested-trycatch