Skip to content

Commit 563c3f8

Browse files
LindnerBrewerygaelcolas
authored andcommittedJul 2, 2024
psconf sildes and demo
1 parent 390bf9b commit 563c3f8

12 files changed

+894
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
. $PSScriptRoot\ErrorFunctions.ps1
2+
3+
############################################################################################################
4+
5+
#region Demo
6+
7+
# Get-ChildItem c:\nonexistantfolder\bla.txt
8+
# 1 / 0
9+
# Import-Excel -Path x:\nonexistingfile.xlsx
10+
11+
# $ErrorView = 'NormalView'
12+
# $ErrorView = 'ConciseView'
13+
14+
#endregion
15+
16+
############################################################################################################
17+
18+
#region Demo Write-Error
19+
20+
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-error?view=powershell-7.4
21+
22+
# Write-Error "MyWriteError"
23+
# "Myerror" | Write-Error
24+
25+
#endregion
26+
27+
############################################################################################################
28+
29+
#region Demo Write-Error in function
30+
31+
# invoke-WriteError -inputObject 'apple', "forbiddenFruit", 'orange'
32+
# Write-Host "Script still running"
33+
34+
#endregion
35+
36+
############################################################################################################
37+
38+
#region Demo Write-Error pipeline
39+
40+
# 'apple', "forbiddenFruit", 'orange' | invoke-WriteError
41+
# Write-Host "Script still running"
42+
43+
#endregion
44+
45+
############################################################################################################
46+
47+
#region Demo Write-Error catch try
48+
49+
# try {
50+
# invoke-WriteError -inputObject 'apple', "forbiddenFruit", 'orange' #-ErrorAction stop
51+
# Write-Host "Script still running"
52+
# }
53+
# catch {
54+
# Write-Host "ran into an error"
55+
# $PSItem
56+
# }
57+
58+
#endregion
59+
60+
############################################################################################################
61+
62+
#region Demo Write-Error control with $erroractionpreference
63+
64+
# $erroractionpreference = 'continue'
65+
# invoke-WriteError -inputObject 'apple', "forbiddenFruit", 'orange'
66+
# Write-Host "Script still running"
67+
68+
#endregion
69+
70+
############################################################################################################
71+
72+
#region Demo Write-Error $?
73+
74+
# invoke-WriteError -inputObject "forbiddenFruit" #-ErrorAction stop
75+
# $?
76+
77+
#endregion
78+
79+
############################################################################################################
80+
81+
#region Demo PsCmdlet.WriteError / try catch
82+
83+
# try {
84+
# Invoke-CmdletWriteError -inputObject 'apple', "forbiddenFruit", 'orange' #-ErrorAction stop
85+
# Write-Output "Script still running"
86+
# }
87+
# catch {
88+
# Write-Output "caught error"
89+
# $_
90+
# }
91+
92+
#endregion
93+
94+
############################################################################################################
95+
96+
#region Demo PsCmdlet.WriteError / $?
97+
98+
# Invoke-CmdletWriteError -inputObject "forbiddenFruit"
99+
# $?
100+
101+
#endregion
102+
103+
############################################################################################################
104+
#region Demo throw terminating error simple
105+
106+
# Invoke-ThrowSimple -inputObject 'apple', "forbiddenFruit", 'orange'
107+
# Write-Host "Script still running"
108+
109+
#endregion
110+
111+
############################################################################################################
112+
113+
#region Demo throw $erroractionpreference and try catch behavior
114+
115+
# Invoke-ThrowSimple -inputObject 'apple', "forbiddenFruit", 'orange' #-ErrorAction SilentlyContinue
116+
# Write-Host "Script still running"
117+
118+
119+
# try {
120+
# Invoke-ThrowSimple -inputObject 'apple', "forbiddenFruit", 'orange' #-ErrorAction SilentlyContinue
121+
# Write-Host "Script still running"
122+
# }
123+
# catch {
124+
# Write-Host "caught error"
125+
# $_
126+
# }
127+
128+
#endregion
129+
130+
############################################################################################################
131+
132+
#region Demo throw exception
133+
134+
# Invoke-ThrowException -inputObject 'apple', "forbiddenFruit", 'orange'
135+
136+
#endregion
137+
138+
############################################################################################################
139+
140+
#region Demo throw error record
141+
142+
# Invoke-ThrowErrorRecord -inputObject 'apple', "forbiddenFruit", 'orange'
143+
144+
#endregion
145+
146+
############################################################################################################
147+
148+
#region Demo PsCmdlet.ThrowTerminatingError / try catch
149+
150+
# Invoke-ThrowTerminatingError -inputObject 'apple', "forbiddenFruit", 'orange' #-ErrorAction SilentlyContinue
151+
# Write-Output "Script still running"
152+
153+
# try {
154+
# Invoke-ThrowTerminatingError -inputObject 'apple', "forbiddenFruit", 'orange'
155+
# Write-Output "Script still running"
156+
# }
157+
# catch {
158+
# Write-Output "caught error"
159+
# $_
160+
# }
161+
162+
#endregion
163+
164+
165+
166+
167+
168+
############################################################################################################
169+
############################################################################################################
170+
############################################################################################################
171+
# Bonus
172+
############################################################################################################
173+
############################################################################################################
174+
#region rethrow error
175+
176+
# Invoke-Rethrow
177+
178+
#endregion
179+
180+
############################################################################################################
181+
182+
#region InnerException
183+
184+
# Invoke-InnerException
185+
186+
#endregion
187+
############################################################################################################
188+
189+
#region Demo catching specific exception
190+
191+
# try {
192+
# Invoke-ThrowTerminatingError -inputObject 'apple', "forbiddenFruit", 'orange' #-ErrorAction stop
193+
# Write-Output "Script still running"
194+
# }catch [System.ArgumentException] {
195+
# Write-Output "caught ArgumentException error"
196+
# $_
197+
# }catch [System.IO.FileNotFoundException] {
198+
# Write-Output "caught FileNotFoundException error"
199+
# $_
200+
# }catch {
201+
# Write-Output "caught other error"
202+
# $_
203+
# }
204+
205+
#endregion
206+
207+
############################################################################################################
208+

0 commit comments

Comments
 (0)