Windows批处理支持异常处理吗?

2024-05-07

Windows批处理编程支持异常处理吗?如果没有,是否有任何方法可以有效地模拟批处理文件中的异常处理?

我希望能够在批处理脚本中的任何 CALL 级别的任何位置“抛出异常”,并重复弹出 CALL 堆栈,直到找到活动的“TRY 块”,然后“CATCH 块”可以处理异常完全并继续,或者进行一些清理并继续弹出调用堆栈。如果从未处理异常,则批处理将终止,控制权将返回到命令行上下文并显示错误消息。

已经有夫妇发布了在任何 CALL 深度终止批处理的方法 https://stackoverflow.com/q/3227796/1012053,但是这些技术都不允许进行任何结构化的清理活动,而这些活动通常是通过异常处理在其他语言中提供的。

Note: 在这种情况下,我已经知道一个最近才发现的好答案,我想分享该信息


Windows 批处理脚本当然没有任何正式的异常处理 - 考虑到这种语言的原始性,这并不奇怪。在我最疯狂的梦想中,我从来没有想过有效的异常处理会被破坏。

但后来一些惊人的发现俄罗斯网站 http://forum.script-coding.com/viewtopic.php?id=9050关于错误的 GOTO 语句的行为(我不知道说什么,我看不懂俄语)。,并对行为进行了进一步调查。

事实证明(GOTO) 2>NUL行为几乎与 EXIT /B 相同,除了已解析的代码块内的串联命令在有效返回后仍然执行, 在调用者的上下文中!

这是一个简短的示例,演示了大部分要点。

@echo off
setlocal enableDelayedExpansion
set "var=Parent Value"
(
  call :test
  echo This and the following line are not executed
  exit /b
)
:break
echo How did I get here^^!^^!^^!^^!
exit /b

:test
setlocal disableDelayedExpansion
set "var=Child Value"
(goto) 2>nul & echo var=!var! & goto :break
echo This line is not executed

:break
echo This line is not executed

- 输出 -

var=Parent Value
How did I get here!!!!

这个功能完全出乎意料,而且非常强大和有用。它已被用于:

  • Create - 'nix here 文档功能的模拟
  • 创建一个RETURN.BAT实用程序 http://www.dostips.com/forum/viewtopic.php?p=41929#p41929任何批处理“函数”都可以方便地调用 CALL 来跨 ENDLOCAL 屏障返回任何值,几乎没有任何限制。该代码是一个充实的版本.

现在我还可以将异常处理添加到列表中:-)

该技术依赖于名为 EXCEPTION.BAT 的批处理实用程序来定义用于指定 TRY/CATCH 块以及引发异常的环境变量“宏”。

在实现 TRY/CATCH 块之前,必须使用以下命令定义宏:

call exception init

然后使用以下语法定义 TRY/CATCH 块:

:calledRoutine
setlocal
%@Try%
  REM normal code goes here
%@EndTry%
:@Catch
  REM Exception handling code goes here
:@EndCatch

可以随时通过以下方式抛出异常:

call exception throw  errorNumber  "messageString"  "locationString"

当抛出异常时,它会使用迭代方式弹出 CALL 堆栈(GOTO) 2>NUL直到它找到一个活动的 TRY/CATCH,然后它分支到 CATCH 块并执行该代码。 CATCH 块可以使用一系列异常属性变量:

  • exception.Code - 数字异常代码
  • exception.Msg - 异常消息字符串
  • exception.Loc - 描述抛出异常的位置的字符串
  • exception.Stack - 一个字符串,用于跟踪从 CATCH 块(或命令行,如果未捕获)的调用堆栈,一直到异常源。

如果异常已完全处理,则应通过以下方式清除异常call exception clear,并且脚本正常进行。如果异常没有完全处理,那么可以用一个全新的异常堆栈抛出一个新的异常,或者可以用保留旧的堆栈

call exception rethrow  errorNumber  "messageString"  "locationString"

如果未处理异常,则打印“未处理的异常”消息,包括四个异常属性,终止所有批处理,并将控制权返回到命令行上下文。

这是使这一切成为可能的代码 - 完整的文档嵌入在脚本中,并且可以通过命令行获取exception help or exception /?.

异常.bat

::EXCEPTION.BAT Version 1.4
::
:: Provides exception handling for Windows batch scripts.
::
:: Designed and written by Dave Benham, with important contributions from
:: DosTips users jeb and siberia-man
::
:: Full documentation is at the bottom of this script
::
:: History:
::   v1.4 2016-08-16  Improved detection of command line delayed expansion
::                    using an original idea by jeb
::   v1.3 2015-12-12  Added paged help option via MORE
::   v1.2 2015-07-16  Use COMSPEC instead of OS to detect delayed expansion
::   v1.1 2015-07-03  Preserve ! in exception attributes when delayed expansion enabled
::   v1.0 2015-06-26  Initial versioned release with embedded documentation
::
@echo off
if "%~1" equ "/??" goto pagedHelp
if "%~1" equ "/?" goto help
if "%~1" equ "" goto help
shift /1 & goto %1


:throw  errCode  errMsg  errLoc
set "exception.Stack="
:: Fall through to :rethrow


:rethrow  errCode  errMsg  errLoc
setlocal disableDelayedExpansion
if not defined exception.Restart set "exception.Stack=[%~1:%~2] %exception.Stack%"
for /f "delims=" %%1 in ("%~1") do for /f "delims=" %%2 in ("%~2") do for /f "delims=" %%3 in ("%~3") do (
  setlocal enableDelayedExpansion
  for /l %%# in (1 1 10) do for /f "delims=" %%S in (" !exception.Stack!") do (
    (goto) 2>NUL
    setlocal enableDelayedExpansion
    if "!!" equ "" (
      endlocal
      setlocal disableDelayedExpansion
      call set "funcName=%%~0"
      call set "batName=%%~f0"
      if defined exception.Restart (set "exception.Restart=") else call set "exception.Stack=%%funcName%%%%S"
      setlocal EnableDelayedExpansion
      if !exception.Try! == !batName!:!funcName! (
        endlocal
        endlocal
        set "exception.Code=%%1"
        if "!!" equ "" (
          call "%~f0" setDelayed
        ) else (
          set "exception.Msg=%%2"
          set "exception.Loc=%%3"
          set "exception.Stack=%%S"
        )
        set "exception.Try="
        (CALL )
        goto :@Catch
      )
    ) else (
      for %%V in (Code Msg Loc Stack Try Restart) do set "exception.%%V="
      if "^!^" equ "^!" (
        call "%~f0" showDelayed
      ) else (
        echo(
        echo Unhandled batch exception:
        echo   Code = %%1
        echo   Msg  = %%2
        echo   Loc  = %%3
        echo   Stack=%%S
      )
      echo on
      call "%~f0" Kill
    )>&2
  )
  set exception.Restart=1
  setlocal disableDelayedExpansion
  call "%~f0" rethrow %1 %2 %3
)
:: Never reaches here


:init
set "@Try=call set exception.Try=%%~f0:%%~0"
set "@EndTry=set "exception.Try=" & goto :@endCatch"
:: Fall through to :clear


:clear
for %%V in (Code Msg Loc Stack Restart Try) do set "exception.%%V="
exit /b


:Kill - Cease all processing, ignoring any remaining cached commands
setlocal disableDelayedExpansion
if not exist "%temp%\Kill.Yes" call :buildYes
call :CtrlC <"%temp%\Kill.Yes" 1>nul 2>&1
:CtrlC
@cmd /c exit -1073741510

:buildYes - Establish a Yes file for the language used by the OS
pushd "%temp%"
set "yes="
copy nul Kill.Yes >nul
for /f "delims=(/ tokens=2" %%Y in (
  '"copy /-y nul Kill.Yes <nul"'
) do if not defined yes set "yes=%%Y"
echo %yes%>Kill.Yes
popd
exit /b


:setDelayed
setLocal disableDelayedExpansion
for %%. in (.) do (
  set "v2=%%2"
  set "v3=%%3"
  set "vS=%%S"
)
(
  endlocal
  set "exception.Msg=%v2:!=^!%"
  set "exception.Loc=%v3:!=^!%"
  set "exception.Stack=%vS:!=^!%"
)
exit /b


:showDelayed -
setLocal disableDelayedExpansion
for %%. in (.) do (
  set "v2=%%2"
  set "v3=%%3"
  set "vS=%%S"
)
for /f "delims=" %%2 in ("%v2:!=^!%") do for /f "delims=" %%3 in ("%v3:!=^!%") do for /f "delims=" %%S in ("%vS:!=^!%") do (
  endlocal
  echo(
  echo Unhandled batch exception:
  echo   Code = %%1
  echo   Msg  = %%2
  echo   Loc  = %%3
  echo   Stack=%%S
)
exit /b


:-?
:help
setlocal disableDelayedExpansion
for /f "delims=:" %%N in ('findstr /rbn ":::DOCUMENTATION:::" "%~f0"') do set "skip=%%N"
for /f "skip=%skip% tokens=1* delims=:" %%A in ('findstr /n "^" "%~f0"') do echo(%%B
exit /b


:-??
:pagedHelp
setlocal disableDelayedExpansion
for /f "delims=:" %%N in ('findstr /rbn ":::DOCUMENTATION:::" "%~f0"') do set "skip=%%N"
((for /f "skip=%skip% tokens=1* delims=:" %%A in ('findstr /n "^" "%~f0"') do @echo(%%B)|more /e) 2>nul
exit /b


:-v
:/v
:version
echo(
for /f "delims=:" %%A in ('findstr "^::EXCEPTION.BAT" "%~f0"') do echo %%A
exit /b


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::DOCUMENTATION:::

EXCEPTION.BAT is a pure batch script utility that provides robust exception
handling within batch scripts. It enables code to be placed in TRY/CATCH blocks.
If no exception is thrown, then only code within the TRY block is executed.
If an exception is thrown, the batch CALL stack is popped repeatedly until it
reaches an active TRY block, at which point control is passed to the associated
CATCH block and normal processing resumes from that point. Code within a CATCH
block is ignored unless an exception is thrown.

An exception may be caught in a different script from where it was thrown.

If no active TRY is found after throwing an exception, then an unhandled
exception message is printed to stderr, all processing is terminated within the
current CMD shell, and control is returned to the shell command line.

TRY blocks are specified using macros. Obviously the macros must be defined
before they can be used. The TRY macros are defined using the following CALL

    call exception init

Besides defining @Try and @EndTry, the init routine also explicitly clears any
residual exception that may have been left by prior processing.

A TRY/CATCH block is structured as follows:

    %@Try%
      REM any normal code goes here
    %@EndTry%
    :@Catch
      REM exception handling code goes here
    :@EndCatch

- Every TRY must have an associated CATCH.

- TRY/CATCH blocks cannot be nested.

- Any script or :labeled routine that uses TRY/CATCH must have at least one
  SETLOCAL prior to the appearance of the first TRY.

- TRY/CATCH blocks use labels, so they should not be placed within parentheses.
  It can be done, but the parentheses block is broken when control is passed to
  the :@Catch or :@EndCatch label, and the code becomes difficult to interpret
  and maintain.

- Any valid code can be used within a TRY or CATCH block, including CALL, GOTO,
  :labels, and balanced parentheses. However, GOTO cannot be used to leave a
  TRY block. GOTO can only be used within a TRY block if the label appears
  within the same TRY block.

- GOTO must never transfer control from outside TRY/CATCH to within a TRY or
  CATCH block.

- CALL should not be used to call a label within a TRY or CATCH block.

- CALLed routines containing TRY/CATCH must have labels that are unique within
  the script. This is generally good batch programming practice anyway.
  It is OK for different scripts to share :label names.

- If a script or routine recursively CALLs itself and contains TRY/CATCH, then
  it must not throw an exception until after execution of the first %@Try%

Exceptions are thrown by using

    call exception throw  Code  Message  Location

where

    Code = The numeric code value for the exception.

    Message = A description of the exception.

    Location = A string that helps identify where the exception occurred.
               Any value may be used. A good generic value is "%~f0[%~0]",
               which expands to the full path of the currently executing
               script, followed by the currently executing routine name
               within square brackets.

The Message and Location values must be quoted if they contain spaces or poison
characters like & | < >. The values must not contain additional internal quotes,
and they must not contain a caret ^.

The following variables will be defined for use by the CATCH block:

  exception.Code  = the Code value
  exception.Msg   = the Message value
  exception.Loc   = the Location value
  exception.Stack = traces the call stack from the CATCH block (or command line
                    if not caught), all the way to the exception.

If the exception is not caught, then all four values are printed as part of the
"unhandled exception" message, and the exception variables are not defined.

A CATCH block should always do ONE of the following at the end:

- If the exception has been handled and processing can continue, then clear the
  exception definition by using

    call exception clear

  Clear should never be used within a Try block.

- If the exception has not been fully handled, then a new exception should be
  thrown which can be caught by a higher level CATCH. You can throw a new
  exception using the normal THROW, which will clear exception.Stack and any
  higher CATCH will have no awareness of the original exception.

  Alternatively, you may rethrow an exception and preserve the exeption stack
  all the way to the original exception:

    call exception rethrow  Code  Message  Location

  It is your choice as to whether you want to pass the original Code and/or
  Message and/or Location. Either way, the stack will preserve all exceptions
  if rethrow is used.

  Rethrow should only be used within a CATCH block.


One last restriction - the full path to EXCEPTION.BAT must not include ! or ^.


This documentation can be accessed via the following commands

    constant stream:   exception /?   OR  exception help
    paged via MORE:    exception /??  OR  exception pagedHelp

The version of this utility can be accessed via

    exception /v  OR  exception version


EXCEPTION.BAT was designed and written by Dave Benham, with important
contributions from DosTips users jeb and siberia-man.

Development history can be traced at:
  http://www.dostips.com/forum/viewtopic.php?f=3&t=6497

下面是测试 EXCEPTION.BAT 功能的脚本。该脚本递归调用自身 7 次。每次迭代都有两个 CALL,一个到 :label,演示正常的异常传播,另一个到脚本,演示跨脚本 CALL 的异常传播。

从递归调用返回时,如果迭代计数是 3 的倍数(迭代 3 和 6),则会引发异常。

每个 CALL 都有自己的异常处理程序,通常会报告异常,然后重新抛出修改后的异常。但如果迭代计数为 5,则处理异常并恢复正常处理。

@echo off

:: Main
setlocal enableDelayedExpansion
if not defined @Try call exception init

set /a cnt+=1
echo Main Iteration %cnt% - Calling :Sub
%@Try%
(
  call :Sub
  call echo Main Iteration %cnt% - :Sub returned %%errorlevel%%
)
%@EndTry%
:@Catch
  setlocal enableDelayedExpansion
  echo(
  echo Main Iteration %cnt% - Exception detected:
  echo   Code     = !exception.code!
  echo   Message  = !exception.msg!
  echo   Location = !exception.loc!
  echo Rethrowing modified exception
  echo(
  endlocal
  call exception rethrow -%cnt% "Main Exception^!" "%~f0<%~0>"
:@EndCatch
echo Main Iteration %cnt% - Exit
exit /b %cnt%


:Sub
setlocal
echo :Sub Iteration %cnt% - Start
%@Try%
  if %cnt% lss 7 (
    echo :Sub Iteration %cnt% - Calling "%~f0"
    call "%~f0"
    %= Show any non-exception return code (demonstrate ERRORLEVEL is preserved if no exception) =%
    call echo :Sub Iteration %cnt% - testException returned %%errorlevel%%
  )
  %= Throw an exception if the iteration count is a multiple of 3 =%
  set /a "1/(cnt%%3)" 2>nul || (
    echo Throwing exception
    call exception throw -%cnt% "Divide by 0 exception^!" "%~f0<%~0>"
  )
%@EndTry%
:@Catch
  setlocal enableDelayedExpansion
  echo(
  echo :Sub Iteration %cnt% - Exception detected:
  echo   Code     = !exception.code!
  echo   Message  = !exception.msg!
  echo   Location = !exception.loc!
  endlocal
  %= Handle the exception if iteration count is a multiple of 5, else rethrow it with new properties =%
  set /a "1/(cnt%%5)" 2>nul && (
    echo Rethrowing modified exception
    echo(
    call exception rethrow -%cnt% ":Sub Exception^!" "%~f0<%~0>"
  ) || (
    call exception clear
    echo Exception handled
    echo(
  )
:@EndCatch
echo :Sub Iteration %cnt% - Exit
exit /b %cnt%

- 输出 -

Main Iteration 1 - Calling :Sub
:Sub Iteration 1 - Start
:Sub Iteration 1 - Calling "C:\test\testException.bat"
Main Iteration 2 - Calling :Sub
:Sub Iteration 2 - Start
:Sub Iteration 2 - Calling "C:\test\testException.bat"
Main Iteration 3 - Calling :Sub
:Sub Iteration 3 - Start
:Sub Iteration 3 - Calling "C:\test\testException.bat"
Main Iteration 4 - Calling :Sub
:Sub Iteration 4 - Start
:Sub Iteration 4 - Calling "C:\test\testException.bat"
Main Iteration 5 - Calling :Sub
:Sub Iteration 5 - Start
:Sub Iteration 5 - Calling "C:\test\testException.bat"
Main Iteration 6 - Calling :Sub
:Sub Iteration 6 - Start
:Sub Iteration 6 - Calling "C:\test\testException.bat"
Main Iteration 7 - Calling :Sub
:Sub Iteration 7 - Start
:Sub Iteration 7 - Exit
Main Iteration 7 - :Sub returned 7
Main Iteration 7 - Exit
:Sub Iteration 6 - testException returned 7
Throwing exception

:Sub Iteration 6 - Exception detected:
  Code     = -6
  Message  = Divide by 0 exception!
  Location = C:\test\testException.bat<:Sub>
Rethrowing modified exception


Main Iteration 6 - Exception detected:
  Code     = -6
  Message  = :Sub Exception!
  Location = C:\test\testException.bat<:Sub>
Rethrowing modified exception


:Sub Iteration 5 - Exception detected:
  Code     = -6
  Message  = Main Exception!
  Location = C:\test\testException.bat<C:\test\testException.bat>
Exception handled

:Sub Iteration 5 - Exit
Main Iteration 5 - :Sub returned 5
Main Iteration 5 - Exit
:Sub Iteration 4 - testException returned 5
:Sub Iteration 4 - Exit
Main Iteration 4 - :Sub returned 4
Main Iteration 4 - Exit
:Sub Iteration 3 - testException returned 4
Throwing exception

:Sub Iteration 3 - Exception detected:
  Code     = -3
  Message  = Divide by 0 exception!
  Location = C:\test\testException.bat<:Sub>
Rethrowing modified exception


Main Iteration 3 - Exception detected:
  Code     = -3
  Message  = :Sub Exception!
  Location = C:\test\testException.bat<:Sub>
Rethrowing modified exception


:Sub Iteration 2 - Exception detected:
  Code     = -3
  Message  = Main Exception!
  Location = C:\test\testException.bat<C:\test\testException.bat>
Rethrowing modified exception


Main Iteration 2 - Exception detected:
  Code     = -2
  Message  = :Sub Exception!
  Location = C:\test\testException.bat<:Sub>
Rethrowing modified exception


:Sub Iteration 1 - Exception detected:
  Code     = -2
  Message  = Main Exception!
  Location = C:\test\testException.bat<C:\test\testException.bat>
Rethrowing modified exception


Main Iteration 1 - Exception detected:
  Code     = -1
  Message  = :Sub Exception!
  Location = C:\test\testException.bat<:Sub>
Rethrowing modified exception


Unhandled batch exception:
  Code = -1
  Msg  = Main Exception!
  Loc  = C:\test\testException.bat<testException>
  Stack= testException [-1:Main Exception!]  :Sub [-1::Sub Exception!]  C:\test\testException.bat [-2:Main Exception!]  :Sub [-2::Sub Exception!]  C:\test\testException.bat [-3:Main Exception!]  :Sub [-3::Sub Exception!]  [-3:Divide by 0 exception!]

最后,这里有一系列简单的脚本,展示了如何有效地使用异常,即使中间脚本对异常一无所知!

从一个简单的除法脚本实用程序开始,该实用程序将两个数字相除并打印结果:

除法.bat

:: divide.bat  numerator  divisor
@echo off
setlocal
set /a result=%1 / %2 2>nul || call exception throw -100 "Division exception" "divide.bat"
echo %1 / %2 = %result%
exit /b

请注意,如果脚本检测到错误,它如何引发异常,但它不执行任何操作来捕获异常。

现在我将编写一个对于批处理异常完全天真的划分测试工具。

测试除法.bat

@echo off
for /l %%N in (4 -1 0) do call divide 12 %%N
echo Finished successfully!

- 输出 -

C:\test>testDivide
12 / 4 = 3
12 / 3 = 4
12 / 2 = 6
12 / 1 = 12

Unhandled batch exception:
  Code = -100
  Msg  = Division exception
  Loc  = divide.bat
  Stack= testDivide divide [-100:Division exception]

请注意最终的 ECHO 永远不会执行,因为 diverge.bat 引发的异常未得到处理。

最后,我将编写一个主脚本来调用朴素的 testDivide 并正确处理异常:

大师.bat

@echo off
setlocal
call exception init

%@Try%
  call testDivide
%@EndTry%
:@Catch
  echo %exception.Msg% detected and handled
  call exception clear
:@EndCatch
echo Finished Successfully!

- 输出 -

C:\test>master
12 / 4 = 3
12 / 3 = 4
12 / 2 = 6
12 / 1 = 12
Division exception detected and handled
Finished Successfully!

主脚本能够成功捕获由divide.bat引发的异常,即使它必须通过testDivide.bat,而testDivide.bat对异常一无所知。很酷 :-)

现在这当然不是解决与错误处理相关的所有问题的灵丹妙药:

  • 内置文档中完整描述了许多语法和代码布局限制。但没有什么太令人震惊的。

  • 没有办法自动将所有错误视为异常。所有异常都必须由代码显式引发。这可能是一件好事,因为错误报告是按惯例处理的——没有严格的规则。有些程序不遵循约定。例如,HELP ValidCommand返回 ERRORLEVEL 1,按照惯例这意味着错误,而HELP InvalidCommand返回 ERRORLEVEL 0,这意味着成功。

  • 这种批处理异常技术无法捕获和处理致命的运行时错误。例如GOTO :NonExistentLabel仍会立即终止所有批处理,而没有任何机会捕获错误。

您可以在以下网址关注Exception.BAT的开发情况:。任何未来的进展都会在那里发布。我可能不会更新这篇 StackOverflow 帖子。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows批处理支持异常处理吗? 的相关文章

  • 批处理文件测试错误级别

    我试图在另一个 exe 成功执行时有条件地从批处理文件有条件地运行一个 exe 我尝试了 IF 和 ERRORLEVEL 的几种不同组合 但似乎都不起作用 TeamBuildTypes Current Branch DatabaseUpda
  • 如何使用 FOR 循环处理批处理文件中的“net use”命令错误输出?

    我在用着net use批处理文件中的命令用于连接远程位置 我想将它的输出存储到一个变量中并对其进行处理 当命令成功完成时 我的代码工作正常 但是 如果出现一些错误 例如密码错误 那么我无法将错误输出存储在变量中 它直接打印到正在运行脚本的控
  • 通过管道连接到 findstr 的输入

    我有一个文本文件 其中包含宏名称列表 每行一个 我的最终目标是打印宏名称在当前目录的文件中出现的次数 宏的名称位于C temp macros txt type C temp macros txt在命令提示符下可以正常打印列表 现在我想将该输
  • 从 ADF Faces JSF 1.2 中的托管 bean 构造函数导航

    是否可以从托管 bean 的构造函数导航到另一个页面 视图 如果发生任何异常 我希望进行此重定向 我尝试了很多方法 Try 1 getFacesContext responseComplete getFacesContext getAppl
  • ASP.NET Web Api 2 中的异常处理

    Problem 我需要处理 Web api 2 异常并返回一个具有正确状态代码的丰富对象 401 表示未经授权 404 表示 ContentNotFound 等 以及一些额外信息作为内容 此外 我需要内容看起来像序列化的Exception对
  • 使用异步任务

    如果这是一个简单的问题 我很抱歉 但我对此很陌生并且仍在学习 我有一个应用程序 当我的用户在输入详细信息后单击按钮登录时 它因 android os NetworkOnMainThreadException 崩溃 我发现这是因为我正在主线程
  • C++,set_terminate 是每个线程本地的吗?

    Should set terminate get terminate在 C 2011 或 C 2003 中为多个线程设置不同的终止异常处理器 例如 如果我有程序并将终止处理程序设置为func 1 然后我启动3个线程 新线程中的终止处理程序是
  • 如何从 HTA 中启动批处理文件

    我有一个基本的 HTA 其中我尝试通过按钮启动批处理文件 但是当我尝试启动批处理文件时 它不会运行 当我按下按钮时 将启动命令提示符窗口 但不会运行脚本 这是代码的简单示例
  • PHP MVC 应用程序中哪里可以捕获异常?

    我有一个中小型 PHP 应用程序 用于练习 OOP 和 MVC 技能 我有初始化 引导程序调用的文件Router谁打电话控制器 gt 服务层 gt 存储库 数据库 然后将变量发送回视图层 所有依赖项均由 DiC IOC 处理 我创建抽象类
  • 在Java中运行bat文件并等待

    您可能会认为从 Java 启动 bat 文件是一项简单的任务 但事实并非如此 我有一个 bat 文件 它对从文本文件读取的值循环执行一些 sql 命令 它或多或少是这样的 FOR F x in CD listOfThings txt do
  • 如何获取Windows批处理的父文件夹

    我正在编写一个批处理文件 我需要获取该bat文件的父文件夹 有可能吗 注意 我的意思是批处理文件的父文件夹 而不是调用该批处理的提示的当前目录 Thanks 批处理的父文件夹位于变量中 dp0位于 例子 echo off setlocal
  • 错误:任务“:app:mergeDebugResources”执行失败。 > java.lang.ArrayIndexOutOfBoundsException(无错误消息)

    你们有人知道 Gradle 构建中的这个异常吗 Error Execution failed for task app mergeDebugResources gt java lang ArrayIndexOutOfBoundsExcept
  • 使用 ELMAH 记录 WCF 服务的异常

    我们正在使用优秀的ELMAH http code google com p elmah处理 ASP NET 3 5 Web 应用程序中未处理的异常 这对于除使用 REST 功能使用的 WCF 服务之外的所有站点都非常有效 当操作方法中发生应
  • 有没有办法在 MS Windows(Powershell 或 CMD)的 ripgrep 中转义引号?

    我想找一个字符串 Hello Hello 以双引号开头 在文本文件中使用ripgrep 通常 在 Bash 或 ZSH 中 这可以通过用反斜杠转义或用单引号括起来来实现 rg Hello rg Hello 然而 在 MS Windows P
  • 如何设置任务在使用 vbs 登录时以当前用户身份运行而无需管理员权限?

    从命令行 我可以创建一个在登录时运行的计划任务 无需管理员权限或用户输入密码来设置任务 但是我必须使用 xml 文件来执行此操作 下面是一个示例 xml 其中 Domain User 部分必须在运行时替换为当前用户的域和名称
  • C++ 获取 catch(...) 块中捕获的异常的描述

    我可以得到捕获的异常的描述吗 catch 堵塞 就像是 what std 异常 您可能可以使用一个技巧 catch handle exception void handle exception try throw catch const s
  • 如何将数据传递给 MATLAB oncleanup 函数?

    我有一个编译好的 matlab 程序 可以自动调整机器参数 在调整周期结束时 我需要恢复一些原始设置 有时会发生意外错误 有时用户会发现调整算法未正常工作 因此应终止 使用 control C 如果发生可预测的错误 我可以使用 try ca
  • 如何捕获 try-with-resource 语句中 close 方法抛出的异常

    我正在读关于try with resourceJava 中的语句可用于指定任意数量的资源 try Resource1 res1 initialize code Resource1 res2 initialize code statement
  • 如何使用批处理文件复制(和增量)文件的多个实例

    我需要创建一个批处理文件来复制文件并在将其放置到目的地时递增它 例子 copy C TEMP MyDoc txt E MyData 本质上 我需要这个复制命令在每次启动时进行复制 现在效果很好 我希望它增加文件名而不是覆盖它 如果我运行此命
  • 通过 PowerShell 运行 .cmd 文件

    我正在尝试使用 PowerShell 在远程服务器上运行 cmd 文件 在我的 ps1 脚本中我尝试过 C MyDirectory MyCommand cmd 它会导致此错误 C MyDirectory MyCommand cmd is n

随机推荐

  • 如何在 CQRS 中处理基于集合的一致性验证?

    我有一个相当简单的域模型 涉及一系列Facility聚合根 鉴于我使用 CQRS 和事件总线来处理从域引发的事件 您如何处理集合的验证 例如 假设我有以下需求 Facility必须有一个唯一的名称 由于我在查询端使用最终一致的数据库 因此在
  • Android:使用 putExtra 从片段访问容器活动对象?

    我正在使用操作栏和片段构建选项卡界面 我需要帮助将数据从容器活动发送到片段 详细地说 我在容器活动中有作业对象 我根据工作对象中的信息创建了几个选项卡 如公司详细信息 经验详细信息等 我需要将作业对象传递给这些片段 以便它可以显示相应的信息
  • VBA 窗体最多可以容纳多少个控件?

    我目前正在构建一个 Excel 2003 应用程序 该应用程序需要非常复杂的表单 并且担心控件数量的限制 目前它有 154 个控件 使用Me Controls Count 这应该是准确的 对吧 但可能只完成了大约三分之一 工作流程确实适合单
  • 如何在 Perl 中使用数组引用中的索引作为方法引用?

    如同这个关于迭代子例程引用的问题 https stackoverflow com questions 452529 how do i iterate over dereference an array of subroutine refs
  • SSL 连接在 MySQL Workbench 中有效,但在 DBeaver 中无效

    为当今读者编辑 这是旧版本 DBeaver 中的一个错误 随后已修复 我尝试在 DBeaver 中使用 SSL 连接到 Google Cloud SQL MySQL 实例时遇到 访问被拒绝 错误 我能够 在 MySQL Workbench
  • 我=我++;未定义。 i = foo(i++) 也未定义吗?

    例如 int foo int i return i int main int i 0 i i Undefined i foo i return 0 对于这种情况 当前的 ISO C 标准会指定什么 EDIT 这是我感到困惑的地方 除非另有说
  • WPF/Silverlight 面试问题? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 您希望某人在 WPF Silverlight 开发工作的面试中了解哪些类型的问题 入门级 强大的 NET 2 0背景并且愿意学习 解释一下依赖属
  • ItemTemplate 与 ControlTemplate

    我对WPF的学习感到困惑 我看到 ControlTemplate 用于确定列表框中每个项目的外观 这不是 ItemTemplate 在列表框中的用途吗 有什么不同 ControlTemplate 允许您更改现有控件的外观和感觉 例如 使普通
  • Yii2:用户身份验证到底是如何工作的? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我不明白Authentication in Yii2 In Yii 1有一个身份类别 我们需要与authentication 方法和调
  • 如何使用 python 将 .docx 文件转换为 html?

    import mammoth f open D filename docx rb document mammoth convert to html f 当我运行此代码时 我无法获取 html 文件 请帮助我获取它 当我转换为 html 文件
  • django自定义模板标签中的访问请求

    我在 myapp extras py 中的代码 from django import template register template Library register inclusion tag new userinfo html d
  • 如何创建链接以打开 Web 部件中的工具部件

    我有一个自定义 Web 部件 当它首次呈现时 我想提供打开修改共享 Web 部件属性的链接 就像我们打开 OOB Web 部件 如内容编辑器或 XML Web 部件 时通常得到的那样 任何人对此都有想法 我正在使用它 但是当我单击它时 它显
  • 错误:外线构造函数不能有模板参数 C++

    我有一些代码看起来像这样Array hpp file template
  • CakePHP 验证错误消息

    我正在尝试修改 CakePHP 显示错误消息的方式 下面是用于显示错误的通用模板 div class error message Please enter at least 3 characters div 我想像下面这样设置错误的样式
  • 动态数据表在asp.net中采用相反的顺序

    在我的网络应用程序中 我创建了 DataTable dt new DataTable dt Columns Add Month 并用日期填充列名 月 Month Column name oct 2014 July 2016 Aug 2016
  • PyDev 无法再调试

    我正在使用 eclipse 4 2 1 和 pydev 2 7 1 以前是 2 6 0 一切都工作正常 直到调试器突然停止工作 它打印 pydev debugger 开始 然后根本不运行程序 而是挂起 根据我在其他问题报告中找到的一些信息
  • 如何避免将相同的片段添加到堆栈中

    我需要一些帮助 他们以这种方式将片段添加到活动中 问题是每次调用 openFragment 时都会创建片段并添加 这是显而易见的 问题 我做了什么修改 这样它只能添加一次片段 在下次使用相同片段标签的调用时 它将什么也不做 案例 第一次按下
  • Delphi - 将字符串从 UTF-8 转换回来

    我在将 UTF 8 编码字符串转换回 delphi 可用的字符串时遇到问题 该应用程序是用 XE8 编写的 并部署在 Windows 和 OSX 上 该应用程序分别在 Windows 和 OSX 上使用 LimeLM API dll 和 d
  • 使用 JavaScript 或 jQuery 设置文本框的最大长度

    我想用 JavaScript 或 jQuery 更改文本框的最大长度 我尝试了以下方法 但似乎没有帮助 var a document getElementsByTagName input for var i 0 i
  • Windows批处理支持异常处理吗?

    Windows批处理编程支持异常处理吗 如果没有 是否有任何方法可以有效地模拟批处理文件中的异常处理 我希望能够在批处理脚本中的任何 CALL 级别的任何位置 抛出异常 并重复弹出 CALL 堆栈 直到找到活动的 TRY 块 然后 CATC