tags:

views:

230

answers:

4

Hi guys,

I am trying to call another JAR's main function. Now, this main function is enclosed under a try and catch block.

But when the main call returns a "NullPointerException" the program just crashes instead of catching it.

So, for example

try {
    somelibary.main()
}
catch (Exception e) {
    System.out.println("Exception Caught");
}

This code dosent catch NullPointerException from the main(). Does anyone know the reason y?

+5  A: 

It's possible that the other main function is doing its own error handling, à la

public static void main(String[] args) {
    try {
        ....
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

If that's what the other main function is doing, then you won't be able to catch its exceptions. Do you have the code for this main function?

Will
Nope, Its a JAR I am using. Is there any workaround this so that I can catch the exception thrown by the main()?
Sunny
@Sunny there is no exception thrown by main, otherwise you'd have caught it. It can be that, as Will says, the exception is caught and an exit is called, but that's different.
extraneon
A: 

With the code you are showing, you will catch also the NullPointerException. But the execution will go on after the catch-block. So the execution of somelibary.main() is stopped in this case. If your main() is not contain more code, the program will end after catching the exception. That's what likely happens. You could post the complete code of your main, to verify this.

Addition: You want to know how to go on with the execution of the program. But in this case somelibrary.main() is stopped by throwing the exception. The only option in the main-class will be a loop, that reexecutes somelibrary.main(). The other possibility is to catch the Exception at some higher level (main input-loop) where you can ignore the problem and go on with the execution of the code. As you say you execute another jars-main I suspect you cannot change the code of the other jar. So you are only left with reexecute the other main:

boolean ended = false;
while (!ended)
{
  try {
      somelibary.main()
      ended = true;
  }
  catch (Exception e) {
      System.out.println("Exception Caught");
  }
}

This code restarts the other main on an exception but ends if the other main ends normally.

Mnementh
+5  A: 

Your code, as shown, will definitely catch a NullPointerException thrown by somelibrary.main(). If the application stops anyway due to a NullPointerException there's a fair chance that somelibrary at some point catches the exception, dumps a stack trace and calls system.exit()

In that case the question is not how to catch a NPE, but how to prevent System.exit() from actually exitting.

And the answer to that question can, of course, be found on StackOverflow to, right here. Just install a SecurityManager before the call to someLibrary, and reset the securityManager afterwards.

extraneon
A: 

Perhaps you could execute the main method in another process using Runtime.exec, then capture the result or the error from out/err stream.

Yardena