Wish Catching Java exceptions based on third party library

aeric

Expert
Licensed User
Longtime User
I can use Try-Catch to handle LastException. Can we catch different type of exceptions based on third party library when using JavaObject?

For now,

LastException returns:
B4X:
(ResourceNotFoundException) org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'test2.vm'

LastException.Message return:
B4X:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'test2.vm'

Can we have something like this? Is this make B4X more complicated?
B4X:
Public Sub putTemplate (tf As String)
    Try
        template = velocity.RunMethod("getTemplate", Array(tf))
    Catch (ResourceNotFoundException)
        Log(ResourceNotFoundException.Message)
    Catch (ParseErrorException)
        Log(ParseErrorException.Message)
    Catch (MethodInvocationException)
        Log(MethodInvocationException.Message)
    Catch (Exception)
        Log(Exception.Message)
    End Try
End Sub

Java sample code:
Java:
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;

Velocity.init();

VelocityContext context = new VelocityContext();

context.put( "name", new String("Velocity") );

Template template = null;

try
{
  template = Velocity.getTemplate("mytemplate.vm");
}
catch( ResourceNotFoundException rnfe )
{
  // couldn't find the template
}
catch( ParseErrorException pee )
{
  // syntax error: problem parsing the template
}
catch( MethodInvocationException mie )
{
  // something invoked in the template
  // threw an exception
}
catch( Exception e )
{}

StringWriter sw = new StringWriter();

template.merge( context, sw );

Maybe there is another way to parse the error message.
Let say I only want to show the final part without showing the java class.
 
Top