0

i am expecting following code to print lines before the statement in which static variable value is set, but it is not working as expected.

import java.io.PrintWriter;
class Bank{
private static boolean isInCrisis = false;
public static boolean getIsInCrisis(){return Bank.isInCrisis;}
public static boolean setIsInCrisis(boolean crisis){
    return Bank.isInCrisis = crisis; 
}
public String getCash() throws Exception{
    if(!Bank.isInCrisis){
        return new String("$100"); 
    }
    else{
        throw new Exception("Bank is in crisis"); 
    }
}
}

public class InstanceObjects{
public static void main(String... st) {
    try{
        Bank hsbc = new Bank();
        PrintWriter writer = new PrintWriter(System.out);
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        hsbc.setIsInCrisis(true);
        writer.printf("collect cash: %s\n",hsbc.getCash());            
        writer.close(); 
    }catch(Exception e){
        e.printStackTrace(); 
    }
}
}

output is throwing the exception "Bank is in crisis", but it should first print some lines of "collect cash ..." message and then the throwed exception message...

3
  • Please include the entire stack trace and indicate the statement in main() where the exception is thrown (should be the second at line). Commented Sep 10, 2013 at 19:59
  • @mabbas , and what about printf statements just above hsbc.setIsInCrisis(true)? output should be like: collect cash $100,...., exception is caught .... bank is in crisis ... Commented Sep 10, 2013 at 20:04
  • java.lang.Exception: Bank is in crisis at Bank.getCash(InstanceObjects.java:13) at InstanceObjects.main(InstanceObjects.java:31) Commented Sep 10, 2013 at 20:05

3 Answers 3

5

The problem is that your PrintWriter is never being flushed. It's accumulating data, but it never writes it to the console because the exception is thrown before its buffer is full.

If you call writer.flush() before the call that throws the exception, you'll see the expected messages.

If you close the writer in a finally block you'll see the data when it's closed, as that flushes the writer too... but after the exception, as catch is executed before finally.

If you use a try-with-resources block instead, you'll see the data before the exception, because there the close happens in a "nested" try/finally, effectively:

try (PrintWriter writer = new PrintWriter(System.out)) {
    Bank hsbc = new Bank();
    ...
} catch(Exception e){
    // writer will already be closed here
    e.printStackTrace(); 
}
Sign up to request clarification or add additional context in comments.

Comments

0

You forgot

writer.flush();

The printf method does not auto flush, your messages are sitting in a buffer that is never sent to the console.

Comments

0

Try flushing the stream before getting to the message that throws the exception.

public class Main{ public static void main(String... st) {
    try{
        Bank hsbc = new Bank();
        PrintWriter writer = new PrintWriter(System.out);
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.printf("collect cash: %s\n",hsbc.getCash());
        writer.flush();
        hsbc.setIsInCrisis(true);
        writer.printf("collect cash: %s\n",hsbc.getCash());            
        writer.close(); 
    }catch(Exception e){
        e.printStackTrace(); 
    } } }

Prints:

collect cash: $100
collect cash: $100
collect cash: $100
collect cash: $100
collect cash: $100
collect cash: $100
collect cash: $100
java.lang.Exception: Bank is in crisis
    at Bank.getCash(Main.java:13)
    at Main.main(Main.java:32)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.