Skip to content

Instantly share code, notes, and snippets.

@chriskuech
Last active July 1, 2019 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriskuech/54113f9f5740e1cd27fb0ee992c1776c to your computer and use it in GitHub Desktop.
Save chriskuech/54113f9f5740e1cd27fb0ee992c1776c to your computer and use it in GitHub Desktop.
$ErrorActionPreference = "Stop"
function Assert-True {
[CmdletBinding()] # add ErrorAction support
Param([boolean]$Value)
if (-not $Value) {
throw "Failed assertion"
}
}
# imperative error handling
try {
Assert-True $false
} catch {
Write-Host "nothing to see here..."
}
# equivalent functional error handling with Nullable monad
if (-not (Assert-True $false -ErrorAction SilentlyContinue)) {
Write-Host "nothing to see here..."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment