![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | trap error in an error Is there a way to trap an error inside of an error handler? Here's a short fictional example. In real life I'm trying to log errors and one of the errors might be I can't connect to the sql server I'm logging to (I still want the message in the file log though). Also note that while the file log in this case gets the error message if I switch the order the error is generated in, I don't get it. (i.e. the file would get the error and SQL wouldn't, but if I was out of disk space I wouldn't get any log even though I could have logged to SQL Server). $logfile = "C:\test.log" function Generate-Error() { &{ &{ 1 / $null } trap [System.Exception] { throw $_.Exception.Message + "`nGenerate-Error()" } } } function Log-Error($msg) { &{ &{ $msg | out-file -filePath $logfile 1 / $null } trap [System.Exception] { } } } &{ &{ Generate-Error } trap [System.Exception] { $list = ($_.Exception.Message + "`nMain()").Split("`n") $list Log-Error $list $host.SetShouldExit(1) exit 1 } } |
My System Specs![]() |
| | #2 (permalink) |
| | Re: trap error in an error On Oct 9, 10:23*am, Larry Charlton <LarryCharl...@xxxxxx> wrote: Quote: > Is there a way to trap an error inside of an error handler? > > Here's a short fictional example. *In real life I'm trying to log errors and > one of the errors might be I can't connect to the sql server I'm logging to > (I still want the message in the file log though). *Also note that while the > file log in this case gets the error message if I switch the order the error > is generated in, I don't get it. *(i.e. the file would get the error and SQL > wouldn't, but if I was out of disk space I wouldn't get any log even though I > could have logged to SQL Server). > > $logfile = "C:\test.log" > > function Generate-Error() > { > * * * * &{ > * * * * * * * * &{ > * * * * * * * * * * * * 1 / $null > * * * * * * * * } > * * * * * * * * trap [System.Exception] { > * * * * * * * * * * * * throw $_.Exception.Message + "`nGenerate-Error()" > * * * * * * * * } > * * * * } > > } > > function Log-Error($msg) > { > * * * * &{ > * * * * * * * * &{ > * * * * * * * * * * * * $msg | out-file -filePath$logfile > * * * * * * * * * * * * 1 / $null > * * * * * * * * } > * * * * * * * * trap [System.Exception] { > * * * * * * * * } > * * * * } > > } > > &{ > * * * * &{ > * * * * * * * * Generate-Error > * * * * } > * * * * trap [System.Exception] > * * * * { > * * * * * * * * $list = ($_.Exception.Message + "`nMain()").Split("`n") > * * * * * * * * $list > * * * * * * * * Log-Error $list > * * * * * * * * $host.SetShouldExit(1) > * * * * * * * * exit 1 > * * * * } > > > > }- Hide quoted text - > > - Show quoted text - which handling is correct: 1) error X occurs a) log error X to sql if error Y occurs logging log X and Y to text file 2) error X occurs a) log error X to sql if error ignore b) log error X to text if error ignore 3) error X occurs a) log error X to sql if error Y occurs log Y to text file b) log error X to text if error Y occurs log Y to sql |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Trap an Error from an EXE | PowerShell | |||
| 80005000 Error trap | VB Script | |||
| Ping error trap? | PowerShell | |||
| why cannot Trap catch error | PowerShell | |||
| How to trap this error in PowerShell | PowerShell | |||