Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: Tim MeagherHi,
So just to confirm, given that the code on the jar file has a "throw" as part of the code we must use the try and catch even if this is not throwing an error.
Yes, assuming that the exception is anything which extends Exception, but isn't a RunnableException, this is true.
Note that this has nothing to do with the Lavastorm Java Node, it is simply required by the Java language.
For further reading, you should probably look at this page as a start:
http://docs.oracle.com/javase/tutori...OrDeclare.html
and I didn't get a fail that this means that there wasn't and error? and that I could write the rest of the code in the try and catch?
This is also correct and a perfectly acceptable way to go, and probably the quickest way to get something working.
In general, though, you probably want to be as specific as possible about the type of error that has occurred and provide as much information back to the person running the node so that they know why the error occurred and how to resolve it.
For this reason, it is often better to either limit the scope of the try/catch block, or handle the different exception types differently.
Since the API you are dealing with just appears to throw generic "Exception"s, handling different exceptions types differently isn't really an option.
Therefore, if you put a try/catch block around everything, you won't be able to easily inform the person running the node which part of the node failed - without resorting to just the method calls and the lines in the stack trace.
Therefore, it's probably better to do something whereby you catch the exceptions for each of the API calls separately, and can then provide more information.
For instance, in your case, you could have something like (assuming that the SessionOptions methods don't throw exceptions) :
SessionOptions options = new SessionOptions();
options.setServerHost("127.0.0.1");
options.setServerPort(8194);
Session session = new Session(options);
if (!session.start()) {
//Assuming that not starting the session will cause other problems later on,
//can log this and fail at this point
logger().error("Error occurred starting the session.");
throw fail();
}
if (!session.openService("//blp/refdata")) {
//Assuming that not being able to open the service will cause other problems later on,
//can log this and fail the node at this point
logger().error("Unable to open the service: //blp/refdata");
throw fail();
}
Service service = null;
try {
service = session.getService("//blp/refdata");
}
catch (Exception e) {
logger().error(e, "An error occurred attempting to get the service: //blp/refdata.
Error:"+e.getMessage());
throw fail();
}
//Now we have the service, and can work with it
//I don't know what you want to do here, so this is just dummy code
//I don't know what things will return either, so that's just dummy code too
try {
SomeResult someResult = service.doSomething();
processSomeResult(someResult);
}
catch (Exception e) {
//Don't know what error could occur here, but I'll assume there is some error that can happen.
//In which case, the exception handling for doing whatever operation it is has been
//separated from the exception handling for getting the service, providing better error messages back to the user
//of the node.
logger().error(e, "An error occurred while doing something.
Error: "+e.getMessage());
throw fail();
}