Forums Register Login

Exception Question

+Pie Number of slices to send: Send
I took SCJP on saturday. I posted this question on certfication results forum, but nobody seems to answer it there, so I am posting it here.

the questions were all ok, except one that i could not think an answer for.

I don't remember it completely, but there was a statement between the try and catch, and should be giving a compiler error. However, the question was a short answer one and value of a string variable was asked. it looked somethng like this:

public class Class
{
String counter = "";

void foo(int i){
try
{
if(i==0)
throw new Exception();
}

counter += "1";
catch
{
//some code
}
finally
{
//some code
}
}
public static void main(String [] args)
{
foo(1);
foo(0); //*****
}

}

what is the value of variable counter at //*****


according to me, the code should not compile since there is a statement between the try/catch.

any comments? if it really can't compile, how should i go forward telling SUN this thing? they should not be penalizing me for this question in that case.
+Pie Number of slices to send: Send
If you're in the US, try this link:

http://suned.sun.com/US/contact_us/index.html

Otherwise, select country from this page:

http://suned.sun.com/

According to my testing, a statement between a try{} block and its associated catch(){} block will result in 2 compile-time errors:
  • 'try' without 'catch' or 'finally'
  • 'catch' without 'try'

  • You'll want to provide Sun with as much specific information as you can, but I also think it's important to isolate the problem in general terms like this...

    [ September 14, 2004: Message edited by: marc weber ]
    +Pie Number of slices to send: Send
    Thanks Marc.

    I will send an email to them stating everything I remember about the question.
    He does not suffer fools gladly. But this tiny ad does:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 621 times.
    Similar Threads
    Passed SCJP 1.4/Help With Question on Exam
    java
    a thread code
    Which executes first? ++ Operator Question.
    A Question for the group
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 19, 2024 02:59:58.