This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quession of append() with try catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Quession of append() with try catch block " Watch "Quession of append() with try catch block " New topic
Author

Quession of append() with try catch block

Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
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

Joined: Nov 29, 2001
Posts: 45
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

Joined: Nov 25, 2001
Posts: 106
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

Joined: Nov 29, 2001
Posts: 45
hi hades
i dont get ur question. could u explain in detail
rgds
vishal
Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
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
 
Have you checked out Aspose?
 
subject: Quession of append() with try catch block
 
It's not a secret anymore!