File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about exception?" Watch "a question about exception?" New topic

a question about exception?

michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
StringBuffer b=new StringBuffer();
catch(Exception a){
what will be the value of ' b' if the method throws NullPointerException?
is the answer 134?

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

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

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).
I agree. Here's the link:
subject: a question about exception?
It's not a secret anymore!