• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

K&S chapter 3 q-6

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



can anybody tell how the code execution takes place
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Since s3 throws exception , it calls up the stack till there is catch to handle the exception .
Answer should be -c
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since s is static so the value of s is now "-"

so s = "-"

okay now going first to the main()

the main() method calls create a ne object which call the s1() method

by going to the s1() method it calls s2() method

by going to s2() method it calls s3() method in its first line

s3() method is a lovely method it only throw an exception

and exception must be handeld or the program will crash

so the s3() quikly send the exception to it's bigger sister s2()

and said "OHH my GOD i have an exception and i'cant handle it please s2() handle it "

s2() accepted

after that s2() realised that she cant handle it so she became angry and confused so she ran quikly to s1()
and said "OHH my GOD i have an exception and i'cant handle it please s1() handle it "

so s1 accepted and the exception catched in the catch statment

and by applying the catch statment
{s+="c"}
and sice the s variable is static
so the s will be equal

"-c"

and the program will terminated without even return to continue the statments left in s2() method


i wish you get it

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI



answer is : -c

do you need explanation on this.

reply me if you need it.

Thanks
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how the exception is going from s3 to s2
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends

s1() is called from the main method. inside s1() s2() is called which is wrapped inside a try block and a catch block is provided so that if any exception is thrown from s2() it can be handled, s2() call s3(). Both s2() and s3() declares exception (according to handle or declare rule for an exception. when throws used and if an exception occurs the execution is thrown back to the calling method). s3() explicitly throws an exception using the throw keyword. As s3() ducks the exception the execution goes back to the caller method (i.e s2()) the method s2() too ducks the exception the control goes back to the caller method s1(). Inside s1() the call to s2() is wrapped inside try and catch hence the catch block is executed and c is appended to - . Hence the output '-c'

Hope this helps
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks sudipto
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome!
reply
    Bookmark Topic Watch Topic
  • New Topic