B4J Question In-Line Java Exception Handling

GuyBooth

Active Member
Licensed User
Longtime User
I have some inline java code that looks like this:
B4X:
public static void setTagContents(Tag tag, FieldKey fieldkey, String sContents) {
    try {
        if (sContents.length() > 0) {
            if (!tag.hasField(fieldkey)) {
                tag.addField(fieldkey, sContents);
            } else {
                tag.setField(fieldkey, sContents);
            }
        }
    }
 catch (Throwable e) {
    e.printStackTrace();
    }               
}
When it throws an error, the result is a very long stack trace in my B4J log. I would prefer to customize it so I have a single line in the log (e.g. "error thrown: Fieldkey does not exist").
Is there a simple way to do I do this?
What is the best way to handle exceptions with in-line java code?
 

Daestrum

Expert
Licensed User
Longtime User
Change
B4X:
e.printStackTrace();

to
B4X:
System.err.println(e.getMessage()); // print exception message to error stream
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
My recommendation is:
B4X:
throw new RuntimeException(e);

Right now I'm using
B4X:
     catch (Throwable e) {
        System.out.println(e.getMessage());
    }
Which allows me to see a single line in the log.

This way you will be able to catch the error in your B4X code.
That would be useful - but how do I access the RuntimeException(e) from within B4J?
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…