Calling the core Java function

I am trying to call another core JAR function. Now this main function is enclosed in a try and catch block.

But when the main call returns a "NullPointerException", the program will simply work, not catch it.

So for example

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

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

+3
source share
4 answers

Your code, as shown, will probably catch the NullPointerException that is thrown somelibrary.main(). If the application still stops due to a NullPointerException, there is a chance that somelibrary will catch the exception at some point, reset the stack trace, and call system.exit ()

, NPE, , System.exit().

, , StackOverflow, . SecurityManager someLibrary reset securityManager .

+5

, , à la

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

, , . ?

+5

, , NullPointerException. . , somelibary.main() . () , . , , . , .

:. , . somelibrary.main() . , somelibrary.main(). , ( ), . , , , . , :

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

, , .

0

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

0
source

Source: https://habr.com/ru/post/1738054/


All Articles