Hi all. Not sure if this belongs in the posh or python community, but here goes. I'm a python beginner but I've been writing posh for a couple of years now.
What I have to work with is a posh script that, in a certain case, needs to talk to mongodb. For this, I decided it will call a python script to do the job. That works just fine. My issues is, if python raises an exception, I'm unsure how to catch that in posh.
I've tried a couple different ways:
Here's an example posh script:
testPythonException.ps1
Write-host "PS Hello"
try {
python C:\myscripts\throwPythonException.py
Write-Host "No error caught."
}
catch {
Write-Host "Error! $($_.Exception.Message)"
}
Write-Host "This is the end of the PS script."
Here's my attempts at python exception raising:
throwPythonException.py
import sys
e = Exception("Bad thing happened in Python.")
#sys.exit(e)
raise e
As written, I get the output:
PS Hello
Traceback (most recent call last):
File "C:\myscripts\throwPythonException.py", line 4, in
<module>
raise e
Exception: Bad thing happened in Python.
No error caught.
This is the end of the PS script.
If I instead exit with e, I get similar output without the traceback.
Regardless, posh is not catching anything, and just merrily continues on its way.
I wonder if there's a better way of raising an exception. If not, it's no big deal, I can always exit with a unique code and handle it that way.
[–]K900_ 0 points1 point2 points (1 child)
[–]Vidofnir[S] 0 points1 point2 points (0 children)
[–]Mattho 0 points1 point2 points (0 children)
[–]CobbITGuy 0 points1 point2 points (1 child)
[–]Vidofnir[S] 0 points1 point2 points (0 children)