简体   繁体   English

在 AHK 中,Try/Catch 和 OnError() 有什么区别? 何时使用其中之一?

[英]In AHK, what is the difference between Try/Catch and OnError()? When to use one or the other?

I am currently writing an AHK script that reads and writes files.我目前正在编写一个读写文件的 AHK 脚本。 I would like to handle the possible I/O errors, but the doc isn't clear to me regarding wether I should use Try/Catch or OnError() .我想处理可能的 I/O 错误,但我不清楚该文档是否应该使用Try/CatchOnError()

What is the difference between the two ?两者有什么区别 And when to use one or the other ?什么时候使用一个或另一个

So, after some more research, here is my understanding:所以,经过一些更多的研究,这是我的理解:

  • Try/Catch : Use it to:尝试/捕捉:用它来:

    • Specifically identify certain lines of code over which it will be applied.具体确定将应用它的某些代码行。
    • Then, if you would like, proceed with the execution.然后,如果您愿意,请继续执行。
    • A Try/Catch allows to proceed with the execution of the command after the Try block that failed. Try/Catch 允许在 Try 块失败后继续执行命令。
    • (But, a Try/Catch does not allow to proceed with the execution of the command after the one that failed within the Try block. For example if 5 commands are wrapped, then if the 2nd one threw, it will not be possible to proceed with the execution from the 3rd after doing something in the Catch-block.) (但是,一个try / catch不允许与所述命令中的一个失败的尝试块后执行继续。例如,如果5级的命令被包裹,则如果第二一个投掷,它不会有可能继续进行在 Catch-block 中执行某些操作后从第 3 次开始执行。)
  • OnError() : Use it to OnError() :使用它

    • Deal with any unhandled error.处理任何未处理的错误。
    • Block (or not) the default error dialog.阻止(或不阻止)默认错误对话框。
    • In any cases the thread execution is stopped after you handled the error.在任何情况下,处理错误后线程执行都会停止。
    • There can be multiple OnError() handlers active at a time, and you can decide, in which order they will be executed (or to stop the execution after any one of them) when an error occurs.一次可以有多个 OnError() 处理程序处于活动状态,您可以决定在发生错误时以何种顺序执行它们(或在其中任何一个之后停止执行)。
    • If all handlers return 0 all handlers are called one after the other, then the default error message is displayed, then the thread exits.如果所有处理程序都返回 0,则所有处理程序都被一个接一个地调用,然后显示默认错误消息,然后线程退出。 If any handler returns a non-zero integer, the thread exits without calling the following handlers and without displaying the default error dialog.如果任何处理程序返回非零整数,则线程退出而不调用以下处理程序并且不显示默认错误对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM