【问题标题】:JMeter Beanshell Error handling using try/catch使用 try/catch 处理 JMeter Beanshell 错误
【发布时间】:2018-10-07 11:52:48
【问题描述】:

我正在尝试使用 beanshell tr​​y catch 脚本,但我遇到了困难。这么几个问题:

为什么当我尝试运行以下代码时收到错误,而不是 catch 语句“beanshell 中的错误?

String str = "a";
try {
    log.info(str)
}

catch (Exception ex) {
    log.error("Error in beanshell", ex);
    throw ex;
}

错误:

2016/12/14 10:26:33 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   In file: inline evaluation of: ``String str = "a"; try {    log.info(str) }  catch (Exception ex) {     log.error("Err . . . '' Encountered "}" at line 4, column 1.

2016/12/14 10:26:33 WARN  - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``String str = "a"; try {    log.info(str) }  catch (Exception ex) {     log.error("Err . . . '' Encountered "}" at line 4, column 1.

其次,当我运行这段代码时:

try {
    String str = "a";
    log.info(str)
}

catch (Exception ex) {
    log.error("Error in beanshell", ex);
    throw ex;
}

我收到此错误:

2016/12/14 10:27:25 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   In file: inline evaluation of: ``try {  String str = "a";   log.info(str) }  catch (Exception ex) {     log.error("Er . . . '' Encountered "}" at line 4, column 1.

2016/12/14 10:27:25 WARN  - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``try {  String str = "a";   log.info(str) }  catch (Exception ex) {     log.error("Er . . . '' Encountered "}" at line 4, column 1.

同样,我不明白为什么 Catch 没有发挥作用。 另外 - Catch Exception/Throwable/EvalError 等有什么区别? 谢谢

【问题讨论】:

    标签: error-handling jmeter try-catch beanshell


    【解决方案1】:

    您正在使用语法(缺少;),因此存在 Java 解释器。异常处理不适用于捕获语法错误。


    尝试以下抛出 ArithmeticException 的示例:

    import java.lang.ArithmeticException;
    
    try {
        int i = 1/0;
    } catch ( ArithmeticException e ) {
        log.info( "print" + e );
    }
    

    参考:

    1. http://www.beanshell.org/manual/syntax.html

    【讨论】:

    • 那么,当我编写脚本时,如何在我的 BeanShell 代码中发现语法错误?我尝试添加您所说的 ArithmeticException 并删除“;”从 int i = 1/0 但 ArithmeticException 没有引发
    • 我不知道我们是否可以将语法错误作为异常处理。上面的代码是一个异常处理的例子,你执行division by 0,它会引发ArithmeticException。仅供参考,没有人出于语法错误的目的使用异常处理。该示例使用有效的语法(添加;),因此,更正语法并尝试,您将看到Arithmetic Exception 将显示为info 而不是BeanShell Error
    【解决方案2】:

    log.info(str) 后面缺少分号,因此您的代码必须是:

    String str = "a";
    try {
        log.info(str);//<-- this semicolon is VERY important
    }
    
    catch (Exception ex) {
        log.error("Error in beanshell", ex);
        throw ex;
    }
    

    如果您的代码无效(由于语法错误或拼写错误而无法正确评估),Beanshell try block 将无济于事

    另一个提示:您可以通过在 Beanshell 脚本的开头添加 debug() 命令来获得额外的日志记录输出。

    有关 Jmeter 中的 Beanshell 脚本的更多信息,请参阅 How to Use BeanShell: JMeter's Favorite Built-in Component 文章

    【讨论】:

    • 如何使用debug()?我只是在脚本的开头添加它?在您添加的链接上,我没有找到使用它的指南。
    • 是的,如果您在脚本开头添加此指令,您将在 STDOUT(启动 JMeter 的控制台窗口)中看到大量输出
    • 如果我仍在调试我的项目并使用 GUI?我应该在哪里看到它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2022-01-24
    • 2012-04-20
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多