The art of throwing errors - Emrys MacInally - PSConfEU 2024

Опубликовано: 14 Март 2026
на канале: PowerShell Conference EU
356
13

In the lecture, we delved into the art of throwing errors in PowerShell. Different types of errors were discussed, such as syntax errors, logical errors, semantic errors, and runtime errors. The focus was on runtime errors, which occur during program execution and include division by zero or file not found exceptions. Non-terminating errors, unique to PowerShell, do not halt code execution. They were explained to be useful, especially in scenarios like retrieving data from multiple servers. Terminating errors, on the other hand, halt code execution unless caught in a try-catch block.

The discussion touched on using writeError to throw non-terminating errors, where examples were shown using writeError and handling its output. The concept of terminating errors was also explored, with the speaker expressing a preference for them over non-terminating errors. The use of 'throw' as a terminating error was demonstrated, showcasing its behavior in different scenarios like try-catch blocks.

A detailed walkthrough was provided on using 'throw' with varying parameters to create specific error records for better error handling. The significance of providing detailed error information to users was emphasized. The speaker recommended avoiding using just a message for error information and instead opting for full error records for better user comprehension.

The importance of proper error handling practices, like using try-catch blocks to safeguard scripts, was highlighted. The speaker also advised against solely relying on non-terminating errors, suggesting terminating errors as a default choice. The usage of 'pscommand' was praised for setting the dollar question mark correctly post-throwing errors.

The lecture concluded with a Q&A session, where topics like terminating errors, halting script execution, limiting user control over error actions, logging errors, and transcript functions in PowerShell were discussed. Attendees were encouraged to explore logging options like the PS3 framework by Fred Weinman for more comprehensive error handling.

Chapters:
00:00:00 The art of throwing errors - Emrys MacInally - PSConfEU 2024
00:00:11 Introduction
00:06:31 Types of Errors
00:11:41 Throwing Non-Terminating Errors
00:15:45 Catch-Try Block for Non-Terminating Errors
00:19:58 Throwing Non-Terminating Errors with a Method
00:22:05 Throwing Terminating Errors
00:24:23 Catch-Try Block for Terminating Errors
00:34:41 Conclusion
00:36:41 Q&A