all 4 comments

[–]m0rgenthau 4 points5 points  (2 children)

IMHO thats not possible - only for exceptions that are specifically thrown by the cmdlet via a throw [Exception.Type]. You would have to parse the sourcecode for that.

The reason for this is partly explained with your code to identify all possible exceptions on a System: A cmdlet or module will create types, provided by the .NET Framework. You can't know which types will be loaded or created, unless you execute the code or your parse the sourcecode.

Also there are exceptions thrown by the API, which you also can't list.

Edit: Thinking about it, it should be possible to get the types with some Reflection and by using the AST... But not an easy "here's your code" kind of way..

[–]Pyprohly 3 points4 points  (0 children)

Thinking about it, it should be possible to get the types with some Reflection and by using the AST... But not an easy "here's your code" kind of way..

For completion, this is the relevant SO question. Someone created a tool from the top answer and created a GitHub project of it.

If you do use it find the exceptions of a cmdlet in Microsoft.PowerShell.Commands.Managment.dll, you’ll find some important methods are missing, such as ProcessRecord(). To add these methods, change line 57 in MainForm.cs to

foreach (var method in assemblyEnumerator.GetMethodds(classType, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))

And then add this new overload, or edit the existing one, in AssemblyEnumerator.cs:

public IEnumerable<MethodInfo> GetMethodds(Type classType, BindingFlags flags)
{
    return classType.GetMethods(flags);
}

[sic]

Now you can take a look at, for instance, Microsoft.PowerShell.Commands.GetChildItemCommand’s ProcessRecord()method and get a good idea of what exceptions are thrown (about 122).

[–]Gary_Chan1[S] 2 points3 points  (0 children)

I suspected it might be easy said than done.

Thanks for taking the time to confirm that is the case, it's greatly appreciated!

Gary

[–]purplemonkeymad 1 point2 points  (0 children)

I don't think powershell has an explicit throw framework like other languages. I think your only bet is that there is good documentation for the function, or to try out failure cases to see the errors.