2

I'm try to run LDA algorithm using mallet library. When I try to run LDA with a set of parameters it's OK but with another set I have this error:

09-Oct-2014 23:50:24.354 INFO [http-nio-8084-exec-127] cc.mallet.topics.ParallelTopicModel.estimate <50> LL/token: -8.73265 
09-Oct-2014 23:50:24.657 INFO [http-nio-8084-exec-127] null.null [beta: 0.00795]  
09-Oct-2014 23:50:24.657 INFO [http-nio-8084-exec-127] null.null <60> LL/token: -8.6299 
09-Oct-2014 23:50:24.957 INFO [http-nio-8084-exec-127] cc.mallet.topics.ParallelTopicModel.estimate <70> LL/token: -8.61982 
09-Oct-2014 23:50:25.019 INFO [http-nio-8084-exec-127] null.null [beta: 0.00583]  
09-Oct-2014 23:50:25.263 INFO [http-nio-8084-exec-127] cc.mallet.topics.ParallelTopicModel.estimate <80> LL/token: -8.89656 
09-Oct-2014 23:50:25.402 INFO [http-nio-8084-exec-127] null.null [beta: 0.00484]

java.lang.ArrayIndexOutOfBoundsException: -1    at 
cc.mallet.topics.WorkerRunnable.sampleTopicsForOneDoc(WorkerRunnable.java:489)  at 
cc.mallet.topics.WorkerRunnable.run(WorkerRunnable.java:275)    at 
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)     at 
java.util.concurrent.FutureTask.run(FutureTask.java:266)    at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)     at 
java.lang.Thread.run(Thread.java:745) java.lang.ArrayIndexOutOfBoundsException: -1  at 
cc.mallet.topics.WorkerRunnable.sampleTopicsForOneDoc(WorkerRunnable.java:489)  at 
cc.mallet.topics.WorkerRunnable.run(WorkerRunnable.java:275)    at 
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)     at 
java.util.concurrent.FutureTask.run(FutureTask.java:266)    at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)     at 
java.lang.Thread.run(Thread.java:745)

My code look like:

try{
  //call some function from library
} catch(Exception e){
   System.out.println("LDA Exception")
}

How can catch exception caused by external jars? I have reed this question but it doesn't work for me. Any Idea?

EDIT:

My project is an restful webservice which it run on apache tomcat Server. I try to call lda algorithm in dopost function.

EDIT 2

Mallet is an open source library. So I tried to read code and I found the below code.

public class ParallelTopicModel implements Serializable {
    int numThreads = 2;
    public void estimate() throws IOException {
        WorkerRunnable[] runnables = new WorkerRunnable[numThreads];
        for (int thread = 0; thread < numThreads; thread++) {
            runnables[thread] = new WorkerRunnable(numTopics, alpha, alphaSum, beta,
                                                   random, data, runnableCounts, 
                                                  runnableTotals, offset, docsPerThread);
        //some code
        }
    }

}

public class WorkerRunnable implements Runnable {

    public void run() {

        try {
            //some code   
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

My webservice:

@POST
@Produces("application/xml")
public String getXml(@FormParam("xmlinput") String xmlinput) throws  Exception {
try {
     //call estimate function in ParallelTopicModel class
     //return  an xml;
} catch (Exception e) {
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<modelingOutput>null</modelingOutput>";
}

So how can handle exceptions whiche produce WorkRunnable class in my web service. I want to ruturn an xml look like

`null

I have read a lot of questions like this and this but I don't found solution

Community
  • 1
  • 1
Jimmysnn
  • 583
  • 4
  • 8
  • 30
  • What is wrong withe code you have written? Does that not catch the exeectpion? – EJK Oct 09 '14 at 21:07
  • My code catch all exception. This exception it is not from my code. It is from jar file – Jimmysnn Oct 09 '14 at 21:08
  • Isn't this just a log-statement? Does your code stop running because the exception bubbles up or are you just bothered because the external jar prints something? – Kulu Limpa Oct 09 '14 at 21:11
  • because the external jar prints error. – Jimmysnn Oct 09 '14 at 21:12
  • If it prints to System.out, there's not much you can do about it. The Exception never enters your code. Why is it a problem if the jar logs information? You can always redirect your `System.out` to a different `PrintStream` but I would advise you not to do that. This also means you won't be able to use the console yourself anymore. **Edit**: It seems that Mallet uses `java.util.logging`, so you may be able to disable console output by adjusting the root logger: http://stackoverflow.com/questions/2533227/how-can-i-disable-the-default-console-handler-while-using-the-java-logging-api – Kulu Limpa Oct 09 '14 at 21:15
  • It is not problem the "System.out.println()". I will handle the exception with another way. But to do this It must enter to the cach statement. – Jimmysnn Oct 09 '14 at 21:22
  • The way I see it, the exception does not enter your catch statement, because it is already handled by Mallet. Though, I might be wrong: Any instance of Throwable can be thrown (and caught), so maybe Mallet is throwing an `Error` instead of an `Exception`. Use `catch(Throwable e){}`, if this does not catch anything, then there is no exception. – Kulu Limpa Oct 09 '14 at 21:28
  • First thing to do is to see what's going on at `WorkerRunnable.java:489`, to see if that gives you any clues. – Hot Licks Oct 09 '14 at 22:47

1 Answers1

5

The problem here is not that the call is to an external jar. Exceptions thrown by any method in your chain of calls, no matter where the actual class's bytecode is stored, are caught at the first catch block up the chain.

The problem you have here is that the exception occurs in a different thread. If you start a separate thread from your code, the exceptions in that thread are not passed on to your thread. As far as your code is concerned, that call has completed. If the code in the other thread doesn't catch them, they'll be caught by that thread's exception handler, if there is such a handler.

Runtime errors are usually caused by bad input, so the usual strategy to avoid them would be to understand why your parameters cause the array index in that method to be negative, and then make sure you never pass such parameters to that method.

If that is impossible, you may create an exception handler for the thread. This would work only if you are the one in control of creating the thread and you can set the handler in it.

You may want to look at this question for more about handling exceptions in threads.

Edit:

Since their WorkerRunnable code seems to catch all exceptions (and print a stack trace), there is no way to catch them yourself. You can do one of two things:

  1. As I said above, check what parameters you passed caused the array out of bounds error, and avoid those conditions. Use an if statement and if the parameters are bad, print your <modelingOutput>null</modelingOutput> output - without running the modeling in the first place.
  2. Use their source, change their catch clause to something that sets a variable that tells you there was an exception, compile it and use that jar instead of theirs. That's what Open Source is for. You may want to communicate with the maintainers of that library and tell them that it would be nice if they added a way to detect if an exception was caused by one of the sub threads.
Community
  • 1
  • 1
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • Thnaks for your answer. please see edit 2. My question is a little different now. – Jimmysnn Oct 11 '14 at 10:57
  • Thnak you for your edit but, 1) I can't validate the parameters because I don't know when it is bad 2) It is very difficult to change their code. I would like to communicate with the maintainers of that library but now I must find a solution. So can I check if thread of other process is alive or crashed? I read this post http://stackoverflow.com/questions/702415/how-to-know-if-other-threads-have-finished and I wonder if can handle exception with something like this – Jimmysnn Oct 11 '14 at 13:28
  • Sorry @Jimmysnn, that will not help you. First, you have no access to these threads, as their references are local variables inside a method that you do not control. But even if you could, there is no way to distinguish between failed run and successful run. Since they have a `try...catch` block, the thread always terminates successfully. It never throws an exception. – RealSkeptic Oct 11 '14 at 15:30