Тёмный

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

Trevor Sullivan
Подписаться 15 тыс.
Просмотров 10 тыс.
50% 1

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.
Microsoft Documentation for PowerShell's try..catch..finally statement: docs.microsoft.com/en-us/powe...
Producer: Trevor Sullivan
Website: trevorsullivan.net
Patreon: / trevorsullivan
Twitter: / pcgeek86
Gab: gab.ai/trevors
Intro music "Amen Tune" used, with permission, from Aaron Static. Check out his music here: / aaronstatic
#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

Наука

Опубликовано:

 

6 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 31   
@vaskatito9462
@vaskatito9462 4 года назад
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!
@ghdbdeeeeeaqqq
@ghdbdeeeeeaqqq 4 года назад
Excellent! As usual, your teaching talents are exceptional! Thank you so much for sharing your videos with us!
@GuidoOliveira
@GuidoOliveira 4 года назад
This camera quality is amazing Trevor! awesome explanation as well!
@GuidoOliveira
@GuidoOliveira 4 года назад
@@TrevorSullivan are you using OBS or Camtasia to screen capture? I failed miserably to use my camlink with camtasia, it records in 5fps, obs works perfectly, but the screen shows some wierd borders =/ This high quality of videos is much appreciated! keep up the awesome work!
@Nunezy1978
@Nunezy1978 Год назад
Exceptional Explanation! Very well explained and helped me understand the aspects i struggled with. Thanks Trevor!
@redadz9105
@redadz9105 2 года назад
The were some improvement regarding Exception Errors in the latest Powershell. The new powershell gives a nicer error
@WindosNZ
@WindosNZ 4 года назад
That pop filter is majestic!
@roybatty3197
@roybatty3197 Год назад
Thanks for explaining, I've got it :)
@SurferSandman
@SurferSandman 2 года назад
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.
@dylanrobey4348
@dylanrobey4348 4 года назад
Very good video. Wish I had found you when I was a Powershell noob!
@stevenls9781
@stevenls9781 3 года назад
Amazing, so well explained. I do like the ErrorActionPreference also, didn't understand that previously. Glad to have found your channel - subscribing now.
@braneshg1167
@braneshg1167 2 года назад
Fantastic! Thanks a lot for the perfect explanation. 👍
@benmathew8173
@benmathew8173 4 года назад
Excellent video,flow throughout the video is good
@BryanR83
@BryanR83 2 года назад
Fantastic, thank you!
@GameHistoryX
@GameHistoryX 3 года назад
Awesome!
@zt.5677
@zt.5677 3 года назад
Thanks!
@VirendraKAS
@VirendraKAS 2 года назад
Very helpful for me
@poprostukanal
@poprostukanal 3 года назад
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?
@Rekke_yt
@Rekke_yt Год назад
Which scenarios would we want to check for the fullyQualifiedErrorId ?
@TheAusafonly
@TheAusafonly 2 года назад
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?
@pawandev1
@pawandev1 Год назад
Thanks for clear explanation Trevor, is Finally in Try Catch similar to End block in Powershell Advance Functions can they be used together?
@TrevorSullivan
@TrevorSullivan Год назад
You're welcome, thank you for watching. And to answer your question, no the end block in PowerShell advanced functions is completely different from the finally statement in a try catch block.
@Semtx552
@Semtx552 4 года назад
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 = 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?
@matthewbramer7654
@matthewbramer7654 4 года назад
Very good content! Was hoping you'd cover $LASTEXITCODE and when that comes into play. Maybe a follow up is in order. 😊
@roybatty3197
@roybatty3197 Год назад
How did You find the right catch [string]?
@cu806
@cu806 3 года назад
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.
@cu806
@cu806 3 года назад
@@TrevorSullivan thank you sir! I tried to pipe write-warning to a text file but the text file was blank. Is this possible?
@cu806
@cu806 3 года назад
Thank you. I will be doing that first thing. Keep the vids coming. I love Powershell. I’m still learning from experts like yourself. Subscribing!
@HaasiAustria
@HaasiAustria 3 года назад
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" }
@ayjay749
@ayjay749 4 года назад
Under normal circumstances, how does PowerShell know when a given exception is terminating or not?
@saiainala2483
@saiainala2483 Год назад
Awesome!
Далее
Stop, Intel’s Already Dead!
13:47
Просмотров 377 тыс.
Переменные в Windows PowerShell 5
20:43
Просмотров 2,4 тыс.
PowerShell Errors and Exceptions Handling
27:49
Просмотров 29 тыс.
АЙФОН Г0ВН0
0:54
Просмотров 1,9 млн
КРУТОЙ ТЕЛЕФОН
0:16
Просмотров 7 млн
АЙФОН Г0ВН0
0:54
Просмотров 1,9 млн