Handling Errors in PowerShell with Try..Catch..Finally

preview_player
Показать описание
The try..catch..finally statements in PowerShell allow you to handle exceptions (errors) in your scripts. One of the unique concepts in PowerShell exceptions is the notion of a terminating error versus a non-terminating error. In this video, we'll explore the difference between both types of exceptions, and learn how to effectively use try..catch..finally to handle exceptions in a calculated manner. In addition, we'll take a look at how to use multiple catch blocks to handle specific types of errors uniquely.

The $ErrorActionPreference variable can be used to control the behavior of PowerShell cmdlets, when an exception is thrown. By setting $ErrorActionPreference to 'stop', you can change non-terminating errors into terminating errors. Only terminating errors are caught by the "try" block, so if you're struggling with a script where try..catch isn't working correctly, you might want to ensure that you're considering terminating versus non-terminating errors as a potential problem.

Producer: Trevor Sullivan

#DevOps #PowerShell #VSCode

0:00 Intro
2:23 Exploring Non-terminating Errors
4:11 Wrap Commands in Try..Catch
8:09 Force Errors to Become Terminating Errors
11:22 Handling Multiple Specific Error Types
15:45 PowerShell Finally Statement
17:38 The $PSItem Automatic Variable
Рекомендации по теме
Комментарии
Автор

Thx a lot man! I'm a beginner and i was starting to loose my temper because my catch was not working... i just had to add the ErrorAction and then everything is smoooth! Thx!

vaskatito
Автор

Exceptional Explanation! Very well explained and helped me understand the aspects i struggled with. Thanks Trevor!

Nunezy
Автор

This was fantastic. Thanks so much for this detailed explanation. I've been using Try/Catch statements, for a while, but was only concerned about any error, I now have a need to look for specific error and this helped me understand how to set that up.

SurferSandman
Автор

Excellent! As usual, your teaching talents are exceptional! Thank you so much for sharing your videos with us!

didier_lu
Автор

Thanks for clear explanation Trevor, is Finally in Try Catch similar to End block in Powershell Advance Functions can they be used together?

pawandev
Автор

This camera quality is amazing Trevor! awesome explanation as well!

GuidoOliveira
Автор

Which scenarios would we want to check for the fullyQualifiedErrorId ?

Rekke_yt
Автор

The were some improvement regarding Exception Errors in the latest Powershell. The new powershell gives a nicer error

redadz
Автор

Thanks for explaining, I've got it :)

roybatty
Автор

Very good video. Wish I had found you when I was a Powershell noob!

dylanrobey
Автор

it's maybe a bit lame, but could you explain in another video how you move around the lines, select whole sentence in quote or close paretasies from end of the line, in the vsc editor?

poprostukanal
Автор

Hello! Do you know, is "finally" removed? I get the error: "the term finally is not recognized as the name of a cmdlet, function, script file, or operable program"
I try this simple example:

try {
Get-Date
}
catch {
"Oh No"
}
finally {
"Ready"
}

HaasiAustria
Автор

Excellent video, flow throughout the video is good

benmathew
Автор

Fantastic! Thanks a lot for the perfect explanation. 👍

braneshg
Автор

Amazing, so well explained. I do like the ErrorActionPreference also, didn't understand that previously. Glad to have found your channel - subscribing now.

stevenls
Автор

How did You find the right catch [string]?

roybatty
Автор

What is all this about write-host kills puppies? I use it in all of my try catch blocks and it produces fine for me. I also cannot get write-information, warning, or error to work in my try catch block lol. Great video.

cu
Автор

Hi Trevor

Please share your secret how you learn about all these details just going through microsoft documentation in detail or there is a secret recipe?

TheAusafonly
Автор

Always nice to see handy things like how someone else uses VSCode
, like shortcuts:

Move line of code up/down = Alt-Arrow Up/Down
Duplicate line = <notbound? I bound it to Ctrl-d>
Comment Out line/selection = Ctrl-/

Thanks for your vids!

To please the YT algorithm, a question:
A "file not found" exception can be handled by using test-path before executing the "try" code.
When do you use try/catch, when do you check for pre-reqs (like test-path) before executing a command?

Semtx
Автор

Under normal circumstances, how does PowerShell know when a given exception is terminating or not?

ayjay