• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can somebody please explain me the output

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

I have come across one more question in java blackbelt

I am not able to understand the output.



The output is BC

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

Originally posted by Padma Asrani:
Hi

I have come across one more question in java blackbelt

I am not able to understand the output.



The output is BC

Regards
PAdma



The method test() does not throw an exception to the main method.

The exception is thrown and caught within the try/catch block inside test() and prints out B. The finally block executes and prints out C.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi padma ji..what is java blackbelt...i donno can you tell me...
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith,

Originally posted by Keith Lynn:


The method test() does not throw an exception to the main method.

The exception is thrown and caught within the try/catch block inside test() and prints out B. The finally block executes and prints out C.



Exception is propagated to main method.The exception which is thrown by finally block in test() method is passed to main method. see following code




Output is
B_B C
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharat Makwana

It's because you are throwing an exception here

finally {
throw new Exception("C");
}


And finally is going to be executed in any case..

So that has to be declare or handle in main()

Hence the code

catch (Exception e) {
System.out.println("B_B " + e.getMessage());
}



will be executed
And you will get
"B_B " + "C"( for e.getMessage())

i.e. B_B C as output..

Regards..
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can anybody can ellaborate and explain the above given program,

how the main method does not throw exception?

Here the method is called in the try catch block as well as the method itself consist of of try catch block,so i am bit confused .
 
khushhal yadav
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani mathur

how the main method does not throw exception?



Because the rule is declare or handle..

Since you are properly handling the exception in main()
by using try and catch block..


You need not to declare them in main using throws keyword..

Regards..
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!!
Khushhal
Thank you for your explanation,i got the point.
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Everyone,

Thanks a lot for the replies but I am still a bit confused about the test method.


When we are inside the test method then once the exception is thrown for the first time then it was caught by the subsequent catch clause but what happens to the exception which was thrown within the catch clause? So after that finally has to be executed, so it again throws the exception which we catching in the main() method. So the output is

B_B C

but again what happens to the exception which was thrown within the catch clause of test method. I thought it should be caught within the main method but indeed it is not.
Can you guys explain me why is that?

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

Originally posted by Bharat Makwana:
Hi Keith,


Exception is propagated to main method.The exception which is thrown by finally block in test() method is passed to main method. see following code




Output is
B_B C



Oops. Sorry. You're right. I didn't read it close enough.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic