首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell错误变量丢失内容

PowerShell错误变量丢失内容
EN

Stack Overflow用户
提问于 2022-05-08 15:04:59
回答 1查看 148关注 0票数 1

我有一个很大的项目,里面有很多功能。只有两个问题:

  1. 关于错误处理的“最佳实践”是什么?要使用每个函数的本地处理,或者只使用Main-Section?
  2. The棘手部分(!)中的一个错误日志记录,让我们看看函数F_XXX中奇怪的错误行为:只有$_传递错误消息,$Error在这里是空的!奇怪的是,当我单独启动函数F_XXX (从模块中删除)时,它的行为与预期的一样,这意味着:$Error返回一个错误。代码:

区块报价

代码语言:javascript
复制
$ErrorActionPreference = "Stop"
Function F1
{
    try
    {
    # do something
    }
    catch
    {
        # 1. cascade Error to Main?
        # throw $Error[0].InnerException
        # or
        # local Error-Logging?
        write-MyErrorLogging -message $Error[0].InnerException
    }
}



Function F2
{
    try
    {
        # do something
    }
    catch
    {
        # 1. cascade Error to Main?
        # throw $Error[0].InnerException
        # or
        # local Error-Logging?
        write-MyErrorLogging -message $Error[0].InnerException
    }
}
Function F_XXXXXX
{
    try
    {
        cls
        write-host "The install data is copied.."
        $share = "\\my_wrong_path\sql_sources\" 
        Copy-Item $share -Destination $installDrive -Force -Recurse
    }
    catch
    {
        $Error[0] #here is nothing!
        $null -eq $Error[0] # here true
        $_.Exception # only here the error-message: wrong path!
    }
}

区块报价

代码语言:javascript
复制
# here Main
try
{
    F1
    F2
    F_XXXXXX
}
catch
{
    write-MyErrorLogging -message $Error[0].InnerException
}

区块引号

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-08 16:33:36

  • Inside a catch ,最好避免$Error[0],因为当前的错误在 .

中得到可靠的反映

代码语言:javascript
复制
- If you do need access to _previous_ errors via the [automatic `$Error` variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#error), **use** **`$global:Error`** **inside modules** - see the bottom section for details.

  • 除非在发生错误时需要执行额外的操作,否则可以让脚本终止(致命的)错误( $ErrorActionPreference = "Stop"语句将代码中的所有错误变为)泡起调用堆栈,直到它被try / catch statement捕获,或者在没有try / catch statement的情况下终止整个调用堆栈(即脚本及其调用方)。

代码语言:javascript
复制
- If you do need to perform additional actions, use `try` / `catch`, and place the actions inside the `catch` block (as well as potential cleanup actions in a `finally` block), followed by _re-throwing_ the error simply by calling `throw` _without an argument_.

因此,您可以在脚本的顶层范围中使用单个try / catch语句:

代码语言:javascript
复制
# Turn all errors in this and descendant scopes into
# script-terminating (fatal) ones.
$ErrorActionPreference = 'Stop'

# ... other function definitions, without try / catch

# Top-level code that calls the other functions and catches
# any errors.
try
{
    F1
    F2
    F_XXXXXX
}
catch
{
    write-MyErrorLogging -message $_.InnerException
}

模块中的

奇怪的是,至少可以达到PowerShell 7.2.3 (本文编写时的当前):

模块中发生的

  • 错误--就像模块外部发生的错误一样--记录在全局范围内存在的$Error变量中。

但是,存在一个看似未使用的、模块-本地复制的$Error variable. 的,其中E 248E 149阴影E 250E 151全局variable.>

解决方法是从模块内部使用$global:Error use

这种行为暗示了一个错误,因为模块本地副本似乎从未被碰过,也没有明显的用途。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72162310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档