Testerone
Testerone

Reputation: 63

JMeter Beanshell Error handling using try/catch

I'm trying to use beanshell try catch scripting but I struggle with it. so few questions:

Why when I try to run the following code I receive error, and not the catch statement "Error in beanshell?

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

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

Error:

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.

Secondly, When I run this code:

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

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

I get this error:

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.

Same, I don't understand why the Catch hasn't come in play. Also - what are is the difference between Catch Exception/Throwable/EvalError, etc? Thanks

Upvotes: 0

Views: 8193

Answers (2)

Dmitri T
Dmitri T

Reputation: 167992

You are missing semicolon after log.info(str) so you code needs to be:

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 won't help if your code is not valid (cannot be properly evaluated due to syntax error or typo)

One more tip: you can get extra logging output by adding debug() command to the beginning of your Beanshell script.

See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in Jmeter

Upvotes: 2

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

you are playing with syntax (missing ;), so Java interpreter exists. Exception Handling is not intended for capturing syntax errors.


try the following example which throws ArithmeticException :

import java.lang.ArithmeticException;

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

Reference:

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

Upvotes: 1

Related Questions