This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Quession of append() with try catch block
 
Similar Threads
Reading image and convert it to Base64 format
how to stop a thread in j2me
a question about exception?
httpconnection on Palm: INET: Couldn't init InetLib -1, 1290
formatting problems with SimpleDateFormat