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...
main()where the exception is thrown (should be the secondatline).hsbc.setIsInCrisis(true)? output should be like: collect cash $100,...., exception is caught .... bank is in crisis ...