Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Not having calling method fail if called method silently fails

    I have a program I inherited where the main class has a main method. For some reason, the program is failing, and it is not even hitting the catch in the main method. I would prefer only to make minimal changes to the existing parts of the program. I have added a class with its own main method, and in a try block it calls the called main method. The entire program silently fails, it does not even hit the catch in the caller's main method.

    I would like my class to not silently fail if the called main method silently fails. Apparently a try-catch is not sufficient for this. Is there a way of doing this?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,189
    Rep Power
    12

    Default Re: Not having calling method fail if called method silently fails

    I assume you have the code (since you know there is a try/catch in the main method). You might try putting debug statements in the program at various places. But you haven't provided much info that would help lead to a solution.
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    program silently fails
    Can you explain what that means?
    Are there catch blocks that don't print any message?
    Or is it a logic problem where the program does nothing with certain inputs?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Norm View Post
    Can you explain what that means?
    Are there catch blocks that don't print any message?
    Or is it a logic problem where the program does nothing with certain inputs?
    Yes, it is not hitting the catch blocks. I put break points both in the catch block and after the whole try-catch block, and it does not hit either.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    it does not hit either.
    If there is no exception, then is there a program logic error that skips over code and exits without doing anything?
    Do some detailed debugging to see where the execution flow is going.

    By "fails" do you mean the program is not doing what you want it to do? Bad logic could do that.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Norm View Post
    If there is no exception, then is there a program logic error that skips over code and exits without doing anything?
    Do some detailed debugging to see where the execution flow is going.

    By "fails" do you mean the program is not doing what you want it to do? Bad logic could do that.
    By "silently fails" I mean here that in the try-catch in the main method that calls the function that does everything, the execution will neither hit the catch block or hit after the call in the try block. The program will terminate. Under normal conditions, I would have expected that either it would hit the catch block or would hit after the try.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    The program will terminate
    Does the code call the System.exit method?

    You can trap calls to the exit method by wrapping the code in a SecurityManager and trapping the call to System.exit and printing the stack trace.
    Last edited by Norm; 02-05-2018 at 03:24 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Norm View Post
    Does the code call the System.exit method?
    Not as far as I can tell. There is a call to that in the program, but it is not hitting that.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    Does the method that is called in the main() method return?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Norm View Post
    Does the method that is called in the main() method return?
    No. FWIW, it's java.lang.Thread.join(). (The threading is part of what's making the debugging difficult.)

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    in the main method that calls the function that does everything
    Not sure I understand. Are you saying the "function" that is called is the join() method?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Norm View Post
    Not sure I understand. Are you saying the "function" that is called is the join() method?
    That was maybe an over-simplification of mine. I misused the word "function" as well. There are 20 lines of code in the main method. Execution does hit line 16, start.join. However, it does not hit 17.

    Java Code:
    public static void main(String[] args) {
            try {
                Configuration configuration = ConfigurationLoader.loadXMLConfigAndLog(CONFIG);
                configuration.unstrictParseArguments(args);
                if (configuration.keyExists(PIPELINE_KEY)) {
                    for (String key : configuration.getChildValues(PIPELINE_KEY)) {
                        ConfigurationLoader.addOverlays(configuration, key, DESTINATION_PATH);
                    }
                } else {
                    System.err.println("No pipelinePart specified.");
                }
                configuration.parseArguments(args);
                MailManager.initalizeMailManager(configuration);
                StageLoader sl = new StageLoader(configuration);
                sl.start();
                sl.join();//Hits here.
                System.out.println("Does not hit here.");
            } catch (Exception e) {
                System.err.println("Does not hit here." + e.getMessage());
                ExitConstants.getExitError(e).exit();
            }
        }
    Last edited by tharpa; 02-05-2018 at 04:14 PM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    What is in StageLoader's run method? (I assume it extends Thread).

    You may need to do some detailed debugging there to see what it is doing.
    Last edited by Norm; 02-05-2018 at 04:19 PM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Not having calling method fail if called method silently fails

    What does this do?
    Java Code:
    ExitConstants.getExitError(e).exit();
    If it calls System.exit() then it's possible that the System.out. is never flushed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    The threading is part of what's making the debugging difficult
    Try replacing the call to sl.start and sl.join with a call to sl.run() to remove the thread.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Norm View Post
    What is in StageLoader's run method? (I assume it extends Thread).

    You may need to do some detailed debugging there to see what it is doing.
    I am doing detailed debugging, but in answer to your question,

    Java Code:
    @Override
    	public void run() {
    		try {
    			if (stageManager.validateConfiguration()) {
    				log.info("Starting initial stages");
    				stageManager.startInitialStages();
    				log.info("Waiting for completion...");
    				(new Thread(pipelineManager, PipelineManager.class.getSimpleName())).start();
    				stageManager.awaitCompletion();
    			}
    			else {
    				ExitConstants.CONFIGURATION_FAILURE.exit();
    			}
    		}
    		catch (Exception e) {
    			ExitConstants.getExitError(e).exit();
    		}
    		ExitConstants.SUCCESS.exit();
    	}

  17. #17
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Tolls View Post
    What does this do?
    Java Code:
    ExitConstants.getExitError(e).exit();
    If it calls System.exit() then it's possible that the System.out. is never flushed.
    It does. The project calls a lot of other projects through its tentacles, and that's how I missed it.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    See post #14. The run() method calls the exit method. ??? What does it do???
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    tharpa is offline Member
    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Default Re: Not having calling method fail if called method silently fails

    Quote Originally Posted by Norm View Post
    See post #14. The run() method calls the exit method. ??? What does it do???
    It does a lot of stuff:

    Java Code:
    lock.readLock().lock();
    		
    		try {
    			for (ExitConstants ec : ExitConstants.values()) {
    				
    				if (ec.exceptions.size() > 0) {
    					System.out.println(ec + " count: " + ec.exceptions.size());
    				}
    				
    				for (Throwable t : ec.exceptions) {
    					t.printStackTrace();
    				}
    			}
    			
    		}
    		finally {
    			lock.readLock().unlock();
    		}
    		
    		if (!serviceMode) {
    			System.out.println("Exiting with " + this.toString() + " (" + this.getExitCode() + ")");
    			System.exit(exitCode);
    		}
    		else {
    			if (!SUCCESS.equals(this)) {
    				throw new RuntimeException("Fatal fail with code <" + this + ">");
    			}
    			else {
    				System.out.println("Not exiting with <" + SUCCESS + "> in service mode");
    				try {
    					throw new Exception();
    				}
    				catch (Exception e) {
    					e.printStackTrace();
    				}
    				
    			}
    		}
    But most significantly is line 22, which answers the specific question. Let me explain why I wanted to know this:

    I am writing a class with a main method which will call the existing method, iterating through all possible parameters and recording whether it was successful or not. But if the called main method is not even receiving notice that what it is calling exited, then neither can my calling main method know that. Unless there is another way I do not know about.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Not having calling method fail if called method silently fails

    main method is not even receiving notice that what it is calling exited
    Using a SecurityManager to trap calls to System.exit() would allow the program to know there won't be a return to the main() method.

    Why didn't you see what was printed by line 21?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 17
    Last Post: 10-23-2014, 08:17 PM
  2. Replies: 2
    Last Post: 07-11-2013, 04:09 AM
  3. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  4. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •