简体   繁体   中英

How to catch exception from external jar in Java

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM