try {
Get-ChildItem -Path '/notexist'
Write-Host "ここが何故か実行される"
} catch {
Write-Host "なぜここに来ない?"
}
▸ 実行ボタンで結果を表示
Source収録記事
この snippet は下の記事に登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
3 件Write-Host "=== Block 1: ErrorAction Continue (デフォルト) ===" Write-Host "[1] try ブロックの直前" try {
▶ 実行可
ハンズオン#1 — Continue (デフォルト)でcatchされない
#07004474673a
Write-Host "=== Block 2: ErrorAction Stop ===" Write-Host "[1] try ブロックの直前" try {
▶ 実行可
ハンズオン#2 — Stopを付けるとcatchが効く
#b12d8ed6f261
# ✗ 中身ゼロ } catch { Write-Host "エラーが発生しました" }
▶ 実行可
③ catch内で$_を見落とす
#a45e7d87bafa