aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception question from kb 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 "exception question from kb" Watch "exception question from kb" New topic
Author

exception question from kb

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
class Plane
{

static String s='-';
public static void main(String []s)
{
new Plane().s1();
System.out.println(s);
}

void s1()
{
try { s2();}
catch(Exception e) { s+="c" ;}
}

void s2()throws Exception
{
s3(); s+="2";
s3(); s+="2b";
}

void s3()throws Exception
{
throw new Exception();
}
}

ans: -c

please expalin me this answer.



Thanks<br />Dinesh
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
Hey, What is the question and what are the given answer options?? Your code wont even compile. You are assigning char to a String. And put the code in code tags and take some time to post the question and answer choices. And quit doing that, if it was your head it would look like a squashed potatoe by now with all the banging you been doing LOL
[ May 10, 2008: Message edited by: sridhar row ]
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Modified code:

class Plane
{

static String s="-";
public static void main(String []s)
{
new Plane().s1();
System.out.println(s);
}

void s1()
{
try { s2();}
catch(Exception e) { s+="c" ;}
}

void s2()throws Exception
{
s3(); s+="2";
s3(); s+="2b";
}

void s3()throws Exception
{
throw new Exception();
}
}

option are :
1.-
2. -c
3.-c2
4.-c22b
Noam Wolf
Ranch Hand

Joined: Jan 12, 2008
Posts: 35
The result is "-c", clearly. The flow of your application is as follows:

in main: create a new Plane() object (Plane has a static string var initialized to "-").

call method s1().

method s1 calls s2 from within a try/catch block

s2 calls s3

s3 throws an exception

now the methods start popping off the stack.

Method s2 bubbles up the exception (which means it STOPS execution, does NOT continue on to s+="2")

Method s1 catches the exception and modifies variable s to "-c"

s1 returns control to main,

Main prints s to screen. "-c"


If you debug your code in eclipse (for example) you can see how the calls from method to method work and you would have a very good understanding of why this is the result.


because .net guys can also write in java
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
Main prints s to screen. "-c"


Main does not print "-c". The main method local parameter s will print some garbage(classname@hashcode value).
Noam Wolf
Ranch Hand

Joined: Jan 12, 2008
Posts: 35
yes, sridhar is correct if you use the exact implementation of the main method as suggested by Dinesh. BUT if your main method uses the name "args" for the arguments passed to main then you won't have a local reference that is actually an array and when you call toString() on it will print out, as sridhar mentioned, the classname@hashcode value.

Another option is to change

to


but I think this is besides the point. The confusion was something more basic which was what I was trying to point out, there is no need to be so negative. If you have nothing to add don't. If you want to point out a mistake be more elaborate so that people can learn from this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception question from kb
 
Similar Threads
K&S chapter 3 q-6
Exception Handing
Exception Handing
Exception Handing
Exception