Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - trap error in an error

Reply
 
Old 10-09-2008   #1 (permalink)
Larry Charlton


 
 

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 SpecsSystem Spec
Old 10-09-2008   #2 (permalink)
RickB


 
 

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 -
I can't be sure I understand.

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46