PoshCode Logo PowerShell Code Repository

Get-Exception (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/827"></script>download | new post

Helps you find the right exception to throw. It can take a filter parameter to filter results down. Usage is Get-Exception -filter MYFILTER like Get-Exception Null. (to port to version 1, just remove the multi-line comments)

  1. <#
  2. .Synopsis
  3.         Outputs .NET exception information along with their summary information if available.
  4. .Description
  5.         Spins all loaded types in all loaded assemblies and will return any Exception types that
  6.         match the filter if specified. Because the script only spins loaded assemblies, if you want
  7.         the script to search additional assemblies, you must load them yourself using
  8.         [System.Reflection.Assembly]::Load or related method.
  9. .Notes
  10. NAME:    Get-Exception
  11. AUTHOR:  David Mohundro
  12. URL:     http://www.mohundro.com/blog/
  13. #>
  14.  
  15. param (
  16.         $filter = ''
  17. )
  18.  
  19. if ($filter -ne '') {
  20.         $filter = ".*$filter"
  21. }
  22.  
  23. $exceptions = [System.AppDomain]::CurrentDomain.GetAssemblies() | foreach {
  24.         $_.GetTypes() | where { $_.FullName -match "System$filter.*Exception$" }
  25. }
  26.  
  27. $exceptions | foreach {
  28.         $type = $_
  29.         $doc = $_.Assembly.Location.Replace("dll", "xml")
  30.         $summary = 'No summary found.'
  31.  
  32.         if (Test-Path $doc) {
  33.                 $xpath = "/doc/members/member[@name='T:$_']"
  34.                 Write-Verbose "Found: $doc"
  35.                 Write-Verbose "XPath to use is: $xpath"
  36.  
  37.                 $xml = [xml]$(Get-Content $doc)
  38.                 $nodes = $xml.SelectNodes($xpath)
  39.  
  40.                 $nodes | foreach {
  41.                         $summary = $_.summary
  42.                 }
  43.         }
  44.  
  45.         $result = New-Object PSObject
  46.  
  47.         $result |
  48.         Add-Member NoteProperty Name $type.FullName -pass |
  49.         Add-Member NoteProperty Summary $summary
  50.  
  51.         $result
  52. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me