How do you make Powershell throw an exception generated inside a function? -


i trying write similar this:

function do-something () {   write-host "starting something"   do-somethingwhichthrowsanexception   write-host "something completed successfully" }  ...  function process-thing () {   try {     do-onething     do-twothing     do-something     do-somethingelse   }   catch {     write-host "exception caught!"   } } 

and following output:

starting {massive red exception text} completed 

why not see "exception caught!"?

why see "something completed successfully"?

does powershell not throw exceptions stack or something?

if so, possible slap submission? have try/catch inside every 1 of do-xxxthing functions?

check article:

so, if want catch errors occur, catch [system.exception] because root of errors. here catch block use:

catch [system.exception] {"caught exception"}

the next thing realize if try something, , not generate terminating error, not move catch block anyway. because default $erroractionpreference (what windows powershell when error arises) continue next command.

in reality, means if error occurs , windows powershell can recover it, attempt execute next command. let know error occurred displaying error console.

if want see windows powershell when non-terminating error arises, @ value of $erroractionpreference variable, example:

ps c:> $erroractionpreference

continue

...

so, thing should change value of $erroractionpreferenceto stop. check answer of link how in script.


Comments