Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quession of append() with try catch block

 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer b=new StringBuffer();
try{
b.append("1");
method();
b.append("2");
}
catch(Exception a){
b.append("3");
}
finally{
b.append("4");}
b.append("5");
what will be the value of ' b' if the method throws
NullPointerException?
My answer is 345 but the real is 1345 Why 1 will be printed?
 
vishal avad
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hades
in ur code exception is caused in method so line b.append("1") which already gets executed.so though the exception is occured then also it is not get rollbacked. rest of the append r obivious
rgds
vishal
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Vshal Avad

Thank for you reply I have a quession on append is that if method append() is a speciall way in throws Exception or others methods will get executed too when they though the excetpion.
 
vishal avad
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hades
i dont get ur question. could u explain in detail
rgds
vishal
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is example of append method and other method
public static void main(String a[])
{
StringBuffer s=new StringBuffer("Java");
String c=new String("Java");
Hello(s,c);
System.out.println(s+c);
}
public static Hello(StringBuffer s, String c)
{
s.append("C");
c.repalce('A','D')
}
Write the Output?
Ans: JavaCJava
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic