aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about exception?" Watch "a question about exception?" New topic
Author

a question about exception?

michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
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?
is the answer 134?


--<br />a java beginner from China
qunlbao leon
Greenhorn

Joined: Jan 16, 2002
Posts: 7
I think the answer should be '1345'
the mothed will cause NullPointerException and
it will be catch in the catch block so b.append(2)
will not be executed.
michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
will b.append("5") run?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
The answer will be 1345.
Since the Exception is being caught, the catch{} block will be run, where "2" gets appended.
The finally block will be run anyway, so "4" gets appended.
The exception is caught and handled by this time. Hence the last line will be run, so "5" gets appended.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: a question about exception?