locked
Repeating error output and error control RRS feed

  • Question

  • Hi All.

    I have the code below, I was just wondering if its possible to have the custom object bit within a function so that it can be repeatable, both inside the try block ad also the catch block.

    I am also wondering why $($_.Exception.Message) is blank, is it better to use $error ?

    try {
    
        $filename = 'F:\test\test.txt'
        $folder = 'F:\test\new'
        New-Item -Path $filename -ItemType File 
        New-Item -Path $folder -ItemType Directory 
    
    #    if ($error.count -gt 0) {$the_error = "Error Message: $($_.Exception.Message)" }
        if ($error.count -gt 0) {$the_error = "Error Message: $($error)" }
    
        $output =  	[pscustomobject]@{
        filename = $filename
        the_error = $the_error    
        }
        
        $json_output = $output | ConvertTo-Json
        write-output $json_output
    }
    catch {
    
        $myerror = "Error Message: $($_.Exception.Message)"
        $myerror = $myerror.ToString()
        $output =  	[pscustomobject]@{
            filename = $filename
            the_error = $the_error    
            }
    
            $json_output = $output | ConvertTo-Json
        Write-Output $json_output
        throw "Error occured"
    }
    
    

    Wednesday, February 12, 2020 7:22 PM

All replies

  • First thing to recognize is that if your New-Item commands fail, they will produce non-terminating error exceptions (access denied, no F:\ drive, etc.). Therefore, they'll never throw an error that can be caught, by your catch block. Each of those commands needs the inclusion of the ErrorAction parameter and the Stop parameter value: ... -ErrorAction Stop. As it sounds as may you understand, a function allows you to call a section of code by using a keyword. Sure, you can place a portion of your repeated code into a function. Move it above the try-catch block where it can be defined before it's used within your script/try-catch. Start with these instructions for now; it may actually correct the blank Exception.Message.
    Wednesday, February 12, 2020 7:49 PM
  • Hi All.

    I have the code below, I was just wondering if its possible to have the custom object bit within a function so that it can be repeatable, both inside the try block ad also the catch block.

    I am also wondering why $($_.Exception.Message) is blank, is it better to use $error ?


    The object is already repeated in both places and that is about the best you can do.

    TO get the message from all errors just use $_.

     $myerror = "Error Message: $_"

    You are calling the error variable by two names and the one you are using is never assigned.  If you format you code correctly all of this would be obvious.


    \_(ツ)_/

    Wednesday, February 12, 2020 9:01 PM
  • This would be the best way to do this:

    $filename = 'F:\test\test.txt'
    $folder = 'F:\test\new'
    $myerror = '' try{ New-Item -Path $filename -ItemType File -ErrorAction Stop New-Item -Path $folder -ItemType Directory -ErrorAction Stop } catch{ $myerror = "Error Message: $_" Write-Host $myerror } finally{ [pscustomobject]@{ filename = $filename Error = $myerror } }

    Of course your code will always cause an error if the folder doesn't already exist or if it already exists.

    Taking time to learn PowerShell before trying to copy code will help you to avoid getting stuck like this.


    \_(ツ)_/


    • Edited by jrv Wednesday, February 12, 2020 9:09 PM
    Wednesday, February 12, 2020 9:08 PM
  • This would be the best way to do this:

    $filename = 'F:\test\test.txt'
    $folder = 'F:\test\new'
    $myerror = '' try{ New-Item -Path $filename -ItemType File -ErrorAction Stop New-Item -Path $folder -ItemType Directory -ErrorAction Stop } catch{ $myerror = "Error Message: $_" Write-Host $myerror } finally{ [pscustomobject]@{ filename = $filename Error = $myerror } }

    Of course your code will always cause an error if the folder doesn't already exist or if it already exists.

    Taking time to learn PowerShell before trying to copy code will help you to avoid getting stuck like this.


    \_(ツ)_/


    Thanks for this.

    What about a scenario whereby if the script fails, I only want to output the error information in json. if it succeeds then I will need more information such as the $filename, this is because it could fail and $filename will have no value and I dont want to return null in json.

    Thursday, February 13, 2020 8:46 AM

  • What about a scenario whereby if the script fails, I only want to output the error information in json. if it succeeds then I will need more information such as the $filename, this is because it could fail and $filename will have no value and I dont want to return null in json.

    Your new3 question doesn't make any technical sense.

    Why would you want to use Json?  It serves no purpose outside of JavaScript.


    \_(ツ)_/

    Thursday, February 13, 2020 8:49 AM

  • What about a scenario whereby if the script fails, I only want to output the error information in json. if it succeeds then I will need more information such as the $filename, this is because it could fail and $filename will have no value and I dont want to return null in json.

    Your new3 question doesn't make any technical sense.

    Why would you want to use Json?  It serves no purpose outside of JavaScript.


    \_(ツ)_/

    Hi

    I output to json as my application only reads json. so whatever is done in Ps needs to be converted to Json so as to be consumed.

    Thursday, February 13, 2020 3:39 PM