Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:17:35+00:00 2026-06-01T12:17:35+00:00

I have wrote the code below which was taken from Java How to program

  • 0

I have wrote the code below which was taken from Java How to program 9th Edition – Paul and Michelle Harvey – The code works fine but the problem is that every time I execute it, it gives me uncertain results in which the exceptions are handled – e.g. please look at the output of the code snippet. can you please help me understand why this behavior is occurring ?

 public class Test {

    public static void main(String[] args) {

        try {
            // call method throwException
            throwException();
        }// end try

        catch (Exception e) {
            System.out.println("Exception handled in main");
        }// end catch

        // call method doesNotThrowException
        doesNotThrowException();

    }

    private static void throwException() throws Exception {
        try {
            System.out.println("Method throwException.");
            throw new Exception(); // generate exception
        }

        catch (Exception exception) {
            System.err.println("Exception handled in method throwException");
            throw exception;
        }

        // executes regardless of what occurs in try ... catch block
        finally {
            System.err.println("Finally executed in throwException.");
        }
    }// end of method throwException

    private static void doesNotThrowException() {
        try {
            System.out.println("Method doesNotThrowException.");
        }

        // catch does not execute as the method does not throw any exceptions
        catch (Exception exception) {
            System.err.println(exception);
        }// end catch

        // executes regardless of what occurs in try ... catch block
        finally {
            System.err.println("Finally executed in doesNotThrowException");
        }
    }// end of deosNotThrowException

}//end Test Class

OUTPUTS:
1)

Method throwException.
Exception handled in method throwException
Finally executed in throwException.
Finally executed in doesNotThrowException
Exception handled in main
Method doesNotThrowException.

2)

Exception handled in method throwException
Finally executed in throwException.Method throwException.

Finally executed in doesNotThrowException
Exception handled in main
Method doesNotThrowException.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:17:36+00:00Added an answer on June 1, 2026 at 12:17 pm

    Different outputs on different runs are because of you’re using 2 different output streams: out and err. It’s up to the OS to flush such I/O streams and it does so in different ways on each run depending on other factors that have nothing to do with your program. The only thing the OS guarantees is that the order for out and the order for err are preserved, but not the order between them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello every one I have wrote a simple code in html5 in which I
I have been refactoring throwaway code which I wrote some years ago in a
i have trouble with optimization of the code below which is changing css property
I have the code below, I'm attempting to write a generic function which takes
The code below is more or less taken from the example MPMoviePlayerController sample code.
I have a particular scenario where I wrote my code using LINQ-SQL but I
I have a problem. I wrote example code and I want to build it
Okay, so i have this code i wrote: class Connection { public static StreamWriter
I've wrote this simple piece of code. And I have a slight problem with
Perl is continuing to surprise me. I have a code which takes an input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.